diff options
author | Pedro Alves <palves@redhat.com> | 2014-04-15 14:02:34 +0100 |
---|---|---|
committer | Pedro Alves <palves@redhat.com> | 2014-04-15 14:23:37 +0100 |
commit | 35e5d2f0f81d97f9bd41586b1979345072b7989d (patch) | |
tree | 7855d882067bbb98544809b10df256ebc2ccc1d3 /gdb | |
parent | eb4c17106b7303565b8d0ce8b572fa64945d1e7d (diff) | |
download | gdb-35e5d2f0f81d97f9bd41586b1979345072b7989d.zip gdb-35e5d2f0f81d97f9bd41586b1979345072b7989d.tar.gz gdb-35e5d2f0f81d97f9bd41586b1979345072b7989d.tar.bz2 |
gdb.base/sym-file.exp, hide guts of the custom loader.
This test uses a simple custom elf loader, implemented in
gdb.base/sym-file-loader.h|c. This loader doesn't have a dlclose-like
function today, but I'll need one. But, I found that the guts of the
loader are exposed too much to the client, making the interface more
complicated than necessary. It's simpler if the loader just exports a
few dlopen/dlsym -style functions. That's what this patch does.
Tested on x86_86 Fedora 17, native and gdbserver.
gdb/testsuite/
2014-04-15 Pedro Alves <palves@redhat.com>
* gdb.base/sym-file-loader.h: Move inclusion of <inttypes.h>,
<ansidecl.h>, <elf/common.h> and <elf/external.h> to
sym-file-loader.c.
(Elf_External_Phdr, Elf_External_Ehdr, Elf_External_Shdr)
(Elf_External_Sym, Elf_Addr, GET, GETADDR, struct segment): Move
to sym-file-loader.c.
(struct library): Forward declare.
(load_shlib, lookup_function): Change prototypes.
(find_shstrtab, find_strtab, find_shdr, find_symtab)
(translate_offset): Remove declarations.
(get_text_addr): New declaration.
* gdb.base/sym-file-loader.c: Move inclusion of <inttypes.h>,
<ansidecl.h>, <elf/common.h> and <elf/external.h> here from
sym-file-loader.h.
(Elf_External_Phdr, Elf_External_Ehdr, Elf_External_Shdr)
(Elf_External_Sym, Elf_Addr, GET, GETADDR, struct segment): Move
here from sym-file-loader.h.
(struct library): New structure.
(load_shlib, lookup_function): Change prototypes and adjust to
work with a struct library.
(find_shstrtab, find_strtab, find_shdr, find_symtab)
(translate_offset): Make static.
(get_text_addr): New function.
* gdb.base/sym-file-main.c (main): Adjust to new loader interface.
Diffstat (limited to 'gdb')
-rw-r--r-- | gdb/testsuite/ChangeLog | 27 | ||||
-rw-r--r-- | gdb/testsuite/gdb.base/sym-file-loader.c | 114 | ||||
-rw-r--r-- | gdb/testsuite/gdb.base/sym-file-loader.h | 77 | ||||
-rw-r--r-- | gdb/testsuite/gdb.base/sym-file-main.c | 28 |
4 files changed, 141 insertions, 105 deletions
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index ca002e9..bbea89d 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,5 +1,32 @@ 2014-04-15 Pedro Alves <palves@redhat.com> + * gdb.base/sym-file-loader.h: Move inclusion of <inttypes.h>, + <ansidecl.h>, <elf/common.h> and <elf/external.h> to + sym-file-loader.c. + (Elf_External_Phdr, Elf_External_Ehdr, Elf_External_Shdr) + (Elf_External_Sym, Elf_Addr, GET, GETADDR, struct segment): Move + to sym-file-loader.c. + (struct library): Forward declare. + (load_shlib, lookup_function): Change prototypes. + (find_shstrtab, find_strtab, find_shdr, find_symtab) + (translate_offset): Remove declarations. + (get_text_addr): New declaration. + * gdb.base/sym-file-loader.c: Move inclusion of <inttypes.h>, + <ansidecl.h>, <elf/common.h> and <elf/external.h> here from + sym-file-loader.h. + (Elf_External_Phdr, Elf_External_Ehdr, Elf_External_Shdr) + (Elf_External_Sym, Elf_Addr, GET, GETADDR, struct segment): Move + here from sym-file-loader.h. + (struct library): New structure. + (load_shlib, lookup_function): Change prototypes and adjust to + work with a struct library. + (find_shstrtab, find_strtab, find_shdr, find_symtab) + (translate_offset): Make static. + (get_text_addr): New function. + * gdb.base/sym-file-main.c (main): Adjust to new loader interface. + +2014-04-15 Pedro Alves <palves@redhat.com> + * gdb.base/sym-file-loader.c: Fix typo. SELF_LINK, not SELK_LINK. 2014-04-15 Pedro Alves <palves@redhat.com> diff --git a/gdb/testsuite/gdb.base/sym-file-loader.c b/gdb/testsuite/gdb.base/sym-file-loader.c index c30ffe1..d10065e 100644 --- a/gdb/testsuite/gdb.base/sym-file-loader.c +++ b/gdb/testsuite/gdb.base/sym-file-loader.c @@ -23,6 +23,59 @@ #include "sym-file-loader.h" +#include <inttypes.h> +#include <ansidecl.h> +#include <elf/common.h> +#include <elf/external.h> + +#ifdef TARGET_LP64 + +typedef Elf64_External_Phdr Elf_External_Phdr; +typedef Elf64_External_Ehdr Elf_External_Ehdr; +typedef Elf64_External_Shdr Elf_External_Shdr; +typedef Elf64_External_Sym Elf_External_Sym; +typedef uint64_t Elf_Addr; + +#elif defined TARGET_ILP32 + +typedef Elf32_External_Phdr Elf_External_Phdr; +typedef Elf32_External_Ehdr Elf_External_Ehdr; +typedef Elf32_External_Shdr Elf_External_Shdr; +typedef Elf32_External_Sym Elf_External_Sym; +typedef uint32_t Elf_Addr; + +#endif + +#define GET(hdr, field) (\ +sizeof ((hdr)->field) == 1 ? (uint64_t) (hdr)->field[0] : \ +sizeof ((hdr)->field) == 2 ? (uint64_t) *(uint16_t *) (hdr)->field : \ +sizeof ((hdr)->field) == 4 ? (uint64_t) *(uint32_t *) (hdr)->field : \ +sizeof ((hdr)->field) == 8 ? *(uint64_t *) (hdr)->field : \ +*(uint64_t *) NULL) + +#define GETADDR(hdr, field) (\ +sizeof ((hdr)->field) == sizeof (Elf_Addr) ? *(Elf_Addr *) (hdr)->field : \ +*(Elf_Addr *) NULL) + +struct segment +{ + uint8_t *mapped_addr; + Elf_External_Phdr *phdr; + struct segment *next; +}; + +struct library +{ + int fd; + Elf_External_Ehdr *ehdr; + struct segment *segments; +}; + +static Elf_External_Shdr *find_shdr (Elf_External_Ehdr *ehdr, + const char *section); +static int translate_offset (uint64_t file_offset, struct segment *seg, + void **addr); + #ifdef TARGET_LP64 uint8_t @@ -123,10 +176,10 @@ get_origin (void) /* Mini shared library loader. No reallocation is performed for the sake of simplicity. */ -int -load_shlib (const char *file, Elf_External_Ehdr **ehdr_out, - struct segment **seg_out) +struct library * +load_shlib (const char *file) { + struct library *lib; uint64_t i; int fd = -1; off_t fsize; @@ -148,7 +201,7 @@ load_shlib (const char *file, Elf_External_Ehdr **ehdr_out, if (origin == NULL) { fprintf (stderr, "get_origin not implemented."); - return -1; + return NULL; } path = alloca (strlen (origin) + 1 + strlen (file) + 1); @@ -162,7 +215,7 @@ load_shlib (const char *file, Elf_External_Ehdr **ehdr_out, if (fd < 0) { perror ("fopen failed."); - return -1; + return NULL; } fsize = lseek (fd, 0, SEEK_END); @@ -170,14 +223,14 @@ load_shlib (const char *file, Elf_External_Ehdr **ehdr_out, if (fsize < 0) { perror ("lseek failed."); - return -1; + return NULL; } addr = (uint8_t *) mmap (NULL, fsize, PROT_READ, MAP_PRIVATE, fd, 0); if (addr == (uint8_t *) -1) { perror ("mmap failed."); - return -1; + return NULL; } /* Check if the lib is an ELF file. */ @@ -188,7 +241,7 @@ load_shlib (const char *file, Elf_External_Ehdr **ehdr_out, || ehdr->e_ident[EI_MAG3] != ELFMAG3) { printf ("Not an ELF file: %x\n", ehdr->e_ident[EI_MAG0]); - return -1; + return NULL; } if (ehdr->e_ident[EI_CLASS] == ELFCLASS32) @@ -196,7 +249,7 @@ load_shlib (const char *file, Elf_External_Ehdr **ehdr_out, if (sizeof (void *) != 4) { printf ("Architecture mismatch."); - return -1; + return NULL; } } else if (ehdr->e_ident[EI_CLASS] == ELFCLASS64) @@ -204,10 +257,19 @@ load_shlib (const char *file, Elf_External_Ehdr **ehdr_out, if (sizeof (void *) != 8) { printf ("Architecture mismatch."); - return -1; + return NULL; } } + lib = malloc (sizeof (struct library)); + if (lib == NULL) + { + printf ("malloc failed."); + return NULL; + } + + lib->fd = fd; + /* Load the program segments. For the sake of simplicity assume that no reallocation is needed. */ phdr = (Elf_External_Phdr *) (addr + GET (ehdr, e_phoff)); @@ -223,8 +285,25 @@ load_shlib (const char *file, Elf_External_Ehdr **ehdr_out, head_seg = next_seg; } } - *ehdr_out = ehdr; - *seg_out = head_seg; + lib->ehdr = ehdr; + lib->segments = head_seg; + return lib; +} + +int +get_text_addr (struct library *lib, void **text_addr) +{ + Elf_External_Shdr *text; + + /* Get the text section. */ + text = find_shdr (lib->ehdr, ".text"); + if (text == NULL) + return -1; + + if (translate_offset (GET (text, sh_offset), lib->segments, text_addr) + != 0) + return -1; + return 0; } @@ -289,7 +368,7 @@ find_strtab (Elf_External_Ehdr *ehdr, /* Return the section header named SECTION. */ -Elf_External_Shdr * +static Elf_External_Shdr * find_shdr (Elf_External_Ehdr *ehdr, const char *section) { uint64_t shstrtab_size = 0; @@ -317,7 +396,7 @@ find_shdr (Elf_External_Ehdr *ehdr, const char *section) /* Return the symbol table. */ -Elf_External_Sym * +static Elf_External_Sym * find_symtab (Elf_External_Ehdr *ehdr, uint64_t *symtab_size) { uint64_t i; @@ -337,7 +416,7 @@ find_symtab (Elf_External_Ehdr *ehdr, uint64_t *symtab_size) /* Translate a file offset to an address in a loaded segment. */ -int +static int translate_offset (uint64_t file_offset, struct segment *seg, void **addr) { while (seg) @@ -369,14 +448,15 @@ translate_offset (uint64_t file_offset, struct segment *seg, void **addr) /* Lookup the address of FUNC. */ int -lookup_function (const char *func, - Elf_External_Ehdr *ehdr, struct segment *seg, void **addr) +lookup_function (struct library *lib, const char *func, void **addr) { const char *strtab; uint64_t strtab_size = 0; Elf_External_Sym *symtab; uint64_t symtab_size = 0; uint64_t i; + Elf_External_Ehdr *ehdr = lib->ehdr; + struct segment *seg = lib->segments; /* Get the string table for the symbols. */ strtab = find_strtab (ehdr, ".strtab", &strtab_size); diff --git a/gdb/testsuite/gdb.base/sym-file-loader.h b/gdb/testsuite/gdb.base/sym-file-loader.h index de9e8be..c6b1af3 100644 --- a/gdb/testsuite/gdb.base/sym-file-loader.h +++ b/gdb/testsuite/gdb.base/sym-file-loader.h @@ -16,84 +16,21 @@ #ifndef __SYM_FILE_LOADER__ #define __SYM_FILE_LOADER__ -#include <inttypes.h> -#include <ansidecl.h> -#include <elf/common.h> -#include <elf/external.h> - -#ifdef TARGET_LP64 - -typedef Elf64_External_Phdr Elf_External_Phdr; -typedef Elf64_External_Ehdr Elf_External_Ehdr; -typedef Elf64_External_Shdr Elf_External_Shdr; -typedef Elf64_External_Sym Elf_External_Sym; -typedef uint64_t Elf_Addr; - -#elif defined TARGET_ILP32 - -typedef Elf32_External_Phdr Elf_External_Phdr; -typedef Elf32_External_Ehdr Elf_External_Ehdr; -typedef Elf32_External_Shdr Elf_External_Shdr; -typedef Elf32_External_Sym Elf_External_Sym; -typedef uint32_t Elf_Addr; - -#endif - -#define GET(hdr, field) (\ -sizeof ((hdr)->field) == 1 ? (uint64_t) (hdr)->field[0] : \ -sizeof ((hdr)->field) == 2 ? (uint64_t) *(uint16_t *) (hdr)->field : \ -sizeof ((hdr)->field) == 4 ? (uint64_t) *(uint32_t *) (hdr)->field : \ -sizeof ((hdr)->field) == 8 ? *(uint64_t *) (hdr)->field : \ -*(uint64_t *) NULL) - -#define GETADDR(hdr, field) (\ -sizeof ((hdr)->field) == sizeof (Elf_Addr) ? *(Elf_Addr *) (hdr)->field : \ -*(Elf_Addr *) NULL) - -struct segment -{ - uint8_t *mapped_addr; - Elf_External_Phdr *phdr; - struct segment *next; -}; +struct library; /* Mini shared library loader. No reallocation is performed for the sake of simplicity. */ -int -load_shlib (const char *file, Elf_External_Ehdr **ehdr_out, - struct segment **seg_out); - -/* Return the section-header table. */ +/* Load a library. */ -Elf_External_Shdr *find_shdrtab (Elf_External_Ehdr *ehdr); +struct library *load_shlib (const char *file); -/* Return the string table of the section headers. */ - -const char *find_shstrtab (Elf_External_Ehdr *ehdr, uint64_t *size); - -/* Return the string table named SECTION. */ - -const char *find_strtab (Elf_External_Ehdr *ehdr, - const char *section, uint64_t *strtab_size); - -/* Return the section header named SECTION. */ - -Elf_External_Shdr *find_shdr (Elf_External_Ehdr *ehdr, const char *section); - -/* Return the symbol table. */ - -Elf_External_Sym *find_symtab (Elf_External_Ehdr *ehdr, - uint64_t *symtab_size); - -/* Translate a file offset to an address in a loaded segment. */ +/* Lookup the address of FUNC. */ -int translate_offset (uint64_t file_offset, struct segment *seg, void **addr); +int lookup_function (struct library *lib, const char *func, void **addr); -/* Lookup the address of FUNC. */ +/* Return the library's loaded text address. */ -int -lookup_function (const char *func, Elf_External_Ehdr* ehdr, - struct segment *seg, void **addr); +int get_text_addr (struct library *lib, void **text_addr); #endif diff --git a/gdb/testsuite/gdb.base/sym-file-main.c b/gdb/testsuite/gdb.base/sym-file-main.c index 2746566..b48a1c2 100644 --- a/gdb/testsuite/gdb.base/sym-file-main.c +++ b/gdb/testsuite/gdb.base/sym-file-main.c @@ -38,37 +38,29 @@ int main (int argc, const char *argv[]) { const char *file = SHLIB_NAME; - Elf_External_Ehdr *ehdr = NULL; - struct segment *head_seg = NULL; - Elf_External_Shdr *text; + struct library *lib; char *text_addr = NULL; int (*pbar) () = NULL; int (*pfoo) (int) = NULL; - if (load_shlib (file, &ehdr, &head_seg) != 0) - return -1; + lib = load_shlib (file); + if (lib == NULL) + return 1; - /* Get the text section. */ - text = find_shdr (ehdr, ".text"); - if (text == NULL) - return -1; - - /* Notify GDB to add the symbol file. */ - if (translate_offset (GET (text, sh_offset), head_seg, (void **) &text_addr) - != 0) - return -1; + if (get_text_addr (lib, (void **) &text_addr) != 0) + return 1; gdb_add_symbol_file (text_addr, file); /* Call bar from SHLIB_NAME. */ - if (lookup_function ("bar", ehdr, head_seg, (void *) &pbar) != 0) - return -1; + if (lookup_function (lib, "bar", (void *) &pbar) != 0) + return 1; (*pbar) (); /* Call foo from SHLIB_NAME. */ - if (lookup_function ("foo", ehdr, head_seg, (void *) &pfoo) != 0) - return -1; + if (lookup_function (lib, "foo", (void *) &pfoo) != 0) + return 1; (*pfoo) (2); |