Skip to content

Commit

Permalink
* addr2line.c (fill_lines): get base addrs in fill_lines to use it
Browse files Browse the repository at this point in the history
  with dladdr_fbases introduced at r45563.
  it didn't get before if the executalbe is not pie.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45587 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nurse committed Apr 14, 2014
1 parent c05940a commit b018825
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Mon Apr 14 18:44:45 2014 NARUSE, Yui <naruse@ruby-lang.org>

* addr2line.c (fill_lines): get base addrs in fill_lines to use it
with dladdr_fbases introduced at r45563.
it didn't get before if the executalbe is not pie.

Mon Apr 14 18:05:48 2014 NARUSE, Yui <naruse@ruby-lang.org>

* addr2line.c (main_exe_path): support FreeBSD.
Expand Down
54 changes: 27 additions & 27 deletions addr2line.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ parse_debug_line(int num_traces, void **traces,
}

/* read file and fill lines */
static void
static uintptr_t
fill_lines(int num_traces, void **traces, int check_debuglink,
obj_info_t **objp, line_info_t *lines, int offset);

Expand Down Expand Up @@ -459,7 +459,7 @@ follow_debuglink(char *debuglink, int num_traces, void **traces,
}

/* read file and fill lines */
static void
static uintptr_t
fill_lines(int num_traces, void **traces, int check_debuglink,
obj_info_t **objp, line_info_t *lines, int offset)
{
Expand All @@ -475,23 +475,24 @@ fill_lines(int num_traces, void **traces, int check_debuglink,
ElfW(Shdr) *symtab_shdr = NULL, *strtab_shdr = NULL;
ElfW(Shdr) *dynsym_shdr = NULL, *dynstr_shdr = NULL;
obj_info_t *obj = *objp;
uintptr_t dladdr_fbase = 0;

fd = open(binary_filename, O_RDONLY);
if (fd < 0) {
return;
goto fail;
}
filesize = lseek(fd, 0, SEEK_END);
if (filesize < 0) {
int e = errno;
close(fd);
kprintf("lseek: %s\n", strerror(e));
return;
goto fail;
}
#if SIZEOF_OFF_T > SIZEOF_SIZE_T
if (filesize > (off_t)SIZE_MAX) {
close(fd);
kprintf("Too large file %s\n", binary_filename);
return;
goto fail;
}
#endif
lseek(fd, 0, SEEK_SET);
Expand All @@ -501,7 +502,7 @@ fill_lines(int num_traces, void **traces, int check_debuglink,
int e = errno;
close(fd);
kprintf("mmap: %s\n", strerror(e));
return;
goto fail;
}

ehdr = (ElfW(Ehdr) *)file;
Expand All @@ -511,10 +512,9 @@ fill_lines(int num_traces, void **traces, int check_debuglink,
* it match non-elf file.
*/
close(fd);
return;
goto fail;
}

if (ehdr->e_type == ET_EXEC) obj->base_addr = 0;
obj->fd = fd;
obj->mapped = file;
obj->mapped_size = (size_t)filesize;
Expand Down Expand Up @@ -557,8 +557,7 @@ fill_lines(int num_traces, void **traces, int check_debuglink,
if (offset == -1) {
/* main executable */
offset = 0;
if (ehdr->e_type != ET_EXEC && dynsym_shdr && dynstr_shdr) {
/* PIE (position-independent executable) */
if (dynsym_shdr && dynstr_shdr) {
char *strtab = file + dynstr_shdr->sh_offset;
ElfW(Sym) *symtab = (ElfW(Sym) *)(file + dynsym_shdr->sh_offset);
int symtab_count = (int)(dynsym_shdr->sh_size / sizeof(ElfW(Sym)));
Expand All @@ -572,11 +571,18 @@ fill_lines(int num_traces, void **traces, int check_debuglink,
s = dlsym(h, strtab + sym->st_name);
if (!s) continue;
if (dladdr(s, &info)) {
obj->base_addr = (uintptr_t)info.dli_fbase;
dladdr_fbase = (uintptr_t)info.dli_fbase;
break;
}
}
} /* otherwise, base address is 0 */
if (ehdr->e_type == ET_EXEC) {
obj->base_addr = 0;
}
else {
/* PIE (position-independent executable) */
obj->base_addr = dladdr_fbase;
}
}
}

if (!symtab_shdr) {
Expand Down Expand Up @@ -613,13 +619,17 @@ fill_lines(int num_traces, void **traces, int check_debuglink,
num_traces, traces,
objp, lines, offset);
}
return;
goto finish;
}

parse_debug_line(num_traces, traces,
file + debug_line_shdr->sh_offset,
debug_line_shdr->sh_size,
obj, lines, offset);
finish:
return dladdr_fbase;
fail:
return (uintptr_t)-1;
}

#define HAVE_MAIN_EXE_PATH
Expand Down Expand Up @@ -674,23 +684,13 @@ rb_dump_backtrace_with_lines(int num_traces, void **traces)
if ((len = main_exe_path()) > 0) {
main_path = (char *)alloca(len + 1);
if (main_path) {
obj_info_t *o;
uintptr_t addr;
memcpy(main_path, binary_filename, len+1);
append_obj(&obj);
o = obj; /* obj may be chaneged with gnu_debuglink */
obj->path = main_path;
fill_lines(num_traces, traces, 1, &obj, lines, -1);

/* set dladdr.dli_fbase */
if (o->base_addr) { /* PIE */
dladdr_fbases[0] = (void *)o->base_addr;
}
else { /* non PIE */
/* base addr is 0, but get dli_fbase to skip with dladdr */
Dl_info info;
if (dladdr(traces[i], &info)) {
dladdr_fbases[0] = info.dli_fbase;
}
addr = fill_lines(num_traces, traces, 1, &obj, lines, -1);
if (addr != (uintptr_t)-1) {
dladdr_fbases[0] = (void *)addr;
}
}
}
Expand Down

0 comments on commit b018825

Please sign in to comment.