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.
This commit is contained in:
parent
eb4c17106b
commit
35e5d2f0f8
4 changed files with 142 additions and 106 deletions
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue