+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.
#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
/* 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;
if (origin == NULL)
{
fprintf (stderr, "get_origin not implemented.");
- return -1;
+ return NULL;
}
path = alloca (strlen (origin) + 1 + strlen (file) + 1);
if (fd < 0)
{
perror ("fopen failed.");
- return -1;
+ return NULL;
}
fsize = lseek (fd, 0, SEEK_END);
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. */
|| 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)
if (sizeof (void *) != 4)
{
printf ("Architecture mismatch.");
- return -1;
+ return NULL;
}
}
else if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
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));
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;
}
/* 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;
/* Return the symbol table. */
-Elf_External_Sym *
+static Elf_External_Sym *
find_symtab (Elf_External_Ehdr *ehdr, uint64_t *symtab_size)
{
uint64_t i;
/* 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)
/* 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);
#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
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);