diff options
Diffstat (limited to 'libdw/dwarf_begin_elf.c')
| -rw-r--r-- | libdw/dwarf_begin_elf.c | 123 |
1 files changed, 108 insertions, 15 deletions
diff --git a/libdw/dwarf_begin_elf.c b/libdw/dwarf_begin_elf.c index 9e944b86..a48dada6 100644 --- a/libdw/dwarf_begin_elf.c +++ b/libdw/dwarf_begin_elf.c @@ -72,6 +72,31 @@ static const char dwarf_scnnames[IDX_last][19] = }; #define ndwarf_scnnames (sizeof (dwarf_scnnames) / sizeof (dwarf_scnnames[0])) +static enum dwarf_type +scn_dwarf_type (Dwarf *result, size_t shstrndx, Elf_Scn *scn) +{ + GElf_Shdr shdr_mem; + GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); + if (shdr == NULL) + return TYPE_UNKNOWN; + + const char *scnname = elf_strptr (result->elf, shstrndx, + shdr->sh_name); + if (scnname != NULL) + { + if (startswith (scnname, ".gnu.debuglto_.debug")) + return TYPE_GNU_LTO; + else if (startswith (scnname, ".debug_") || startswith (scnname, ".zdebug_")) + { + size_t len = strlen (scnname); + if (strcmp (scnname + len - 4, ".dwo") == 0) + return TYPE_DWO; + else + return TYPE_PLAIN; + } + } + return TYPE_UNKNOWN; +} static Dwarf * check_section (Dwarf *result, size_t shstrndx, Elf_Scn *scn, bool inscngrp) { @@ -116,7 +141,11 @@ check_section (Dwarf *result, size_t shstrndx, Elf_Scn *scn, bool inscngrp) return NULL; } - /* Recognize the various sections. Most names start with .debug_. */ + /* Recognize the various sections. Most names start with .debug_. + They might be compressed (and start with .z). Or end with .dwo + for split dwarf sections. Or start with .gnu.debuglto_ for + LTO debug sections. We should only use one consistent set at + a time. We prefer PLAIN over DWO over LTO. */ size_t cnt; bool gnu_compressed = false; for (cnt = 0; cnt < ndwarf_scnnames; ++cnt) @@ -127,7 +156,15 @@ check_section (Dwarf *result, size_t shstrndx, Elf_Scn *scn, bool inscngrp) && (dbglen == scnlen || (scnlen == dbglen + 4 && strstr (scnname, ".dwo") == scnname + dbglen))) - break; + { + if (dbglen == scnlen) + { + if (result->type == TYPE_PLAIN) + break; + } + else if (result->type == TYPE_DWO) + break; + } else if (scnname[0] == '.' && scnname[1] == 'z' && (strncmp (&scnname[2], &dwarf_scnnames[cnt][1], dbglen - 1) == 0 @@ -136,13 +173,27 @@ check_section (Dwarf *result, size_t shstrndx, Elf_Scn *scn, bool inscngrp) && strstr (scnname, ".dwo") == scnname + dbglen + 1)))) { - gnu_compressed = true; - break; + if (scnlen == dbglen + 1) + { + if (result->type == TYPE_PLAIN) + { + gnu_compressed = true; + break; + } + } + else if (result->type <= TYPE_DWO) + { + gnu_compressed = true; + break; + } } else if (scnlen > 14 /* .gnu.debuglto_ prefix. */ && startswith (scnname, ".gnu.debuglto_") && strcmp (&scnname[14], dwarf_scnnames[cnt]) == 0) - break; + { + if (result->type == TYPE_GNU_LTO) + break; + } } if (cnt >= ndwarf_scnnames) @@ -224,12 +275,29 @@ valid_p (Dwarf *result) result = NULL; } + /* We are setting up some "fake" CUs, which need an address size. + Check the ELF class to come up with something reasonable. */ + int elf_addr_size = 8; + if (result != NULL) + { + GElf_Ehdr ehdr; + if (gelf_getehdr (result->elf, &ehdr) == NULL) + { + Dwarf_Sig8_Hash_free (&result->sig8_hash); + __libdw_seterrno (DWARF_E_INVALID_ELF); + free (result); + result = NULL; + } + else if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) + elf_addr_size = 4; + } + /* For dwarf_location_attr () we need a "fake" CU to indicate where the "fake" attribute data comes from. This is a block inside the .debug_loc or .debug_loclists section. */ if (result != NULL && result->sectiondata[IDX_debug_loc] != NULL) { - result->fake_loc_cu = (Dwarf_CU *) malloc (sizeof (Dwarf_CU)); + result->fake_loc_cu = malloc (sizeof (Dwarf_CU)); if (unlikely (result->fake_loc_cu == NULL)) { Dwarf_Sig8_Hash_free (&result->sig8_hash); @@ -247,15 +315,16 @@ valid_p (Dwarf *result) = (result->sectiondata[IDX_debug_loc]->d_buf + result->sectiondata[IDX_debug_loc]->d_size); result->fake_loc_cu->locs = NULL; - result->fake_loc_cu->address_size = 0; - result->fake_loc_cu->version = 0; + result->fake_loc_cu->address_size = elf_addr_size; + result->fake_loc_cu->offset_size = 4; + result->fake_loc_cu->version = 4; result->fake_loc_cu->split = NULL; } } if (result != NULL && result->sectiondata[IDX_debug_loclists] != NULL) { - result->fake_loclists_cu = (Dwarf_CU *) malloc (sizeof (Dwarf_CU)); + result->fake_loclists_cu = malloc (sizeof (Dwarf_CU)); if (unlikely (result->fake_loclists_cu == NULL)) { Dwarf_Sig8_Hash_free (&result->sig8_hash); @@ -274,8 +343,9 @@ valid_p (Dwarf *result) = (result->sectiondata[IDX_debug_loclists]->d_buf + result->sectiondata[IDX_debug_loclists]->d_size); result->fake_loclists_cu->locs = NULL; - result->fake_loclists_cu->address_size = 0; - result->fake_loclists_cu->version = 0; + result->fake_loclists_cu->address_size = elf_addr_size; + result->fake_loclists_cu->offset_size = 4; + result->fake_loclists_cu->version = 5; result->fake_loclists_cu->split = NULL; } } @@ -286,7 +356,7 @@ valid_p (Dwarf *result) inside the .debug_addr section, if it exists. */ if (result != NULL && result->sectiondata[IDX_debug_addr] != NULL) { - result->fake_addr_cu = (Dwarf_CU *) malloc (sizeof (Dwarf_CU)); + result->fake_addr_cu = malloc (sizeof (Dwarf_CU)); if (unlikely (result->fake_addr_cu == NULL)) { Dwarf_Sig8_Hash_free (&result->sig8_hash); @@ -306,8 +376,9 @@ valid_p (Dwarf *result) = (result->sectiondata[IDX_debug_addr]->d_buf + result->sectiondata[IDX_debug_addr]->d_size); result->fake_addr_cu->locs = NULL; - result->fake_addr_cu->address_size = 0; - result->fake_addr_cu->version = 0; + result->fake_addr_cu->address_size = elf_addr_size; + result->fake_addr_cu->offset_size = 4; + result->fake_addr_cu->version = 5; result->fake_addr_cu->split = NULL; } } @@ -324,6 +395,16 @@ global_read (Dwarf *result, Elf *elf, size_t shstrndx) { Elf_Scn *scn = NULL; + /* First check the type (PLAIN, DWO, LTO) we are looking for. We + prefer PLAIN if available over DWO, over LTO. */ + while ((scn = elf_nextscn (elf, scn)) != NULL && result->type != TYPE_PLAIN) + { + enum dwarf_type type = scn_dwarf_type (result, shstrndx, scn); + if (type > result->type) + result->type = type; + } + + scn = NULL; while (result != NULL && (scn = elf_nextscn (elf, scn)) != NULL) result = check_section (result, shstrndx, scn, false); @@ -368,6 +449,9 @@ scngrp_read (Dwarf *result, Elf *elf, size_t shstrndx, Elf_Scn *scngrp) represent section indices. The first word is a flag word. */ Elf32_Word *scnidx = (Elf32_Word *) data->d_buf; size_t cnt; + + /* First check the type (PLAIN, DWO, LTO) we are looking for. We + prefer PLAIN if available over DWO, over LTO. */ for (cnt = 1; cnt * sizeof (Elf32_Word) <= data->d_size; ++cnt) { Elf_Scn *scn = elf_getscn (elf, scnidx[cnt]); @@ -381,6 +465,15 @@ scngrp_read (Dwarf *result, Elf *elf, size_t shstrndx, Elf_Scn *scngrp) return NULL; } + enum dwarf_type type = scn_dwarf_type (result, shstrndx, scn); + if (type > result->type) + result->type = type; + } + + for (cnt = 1; cnt * sizeof (Elf32_Word) <= data->d_size && result != NULL; ++cnt) + { + Elf_Scn *scn = elf_getscn (elf, scnidx[cnt]); + assert (scn != NULL); // checked above result = check_section (result, shstrndx, scn, true); if (result == NULL) break; @@ -415,7 +508,7 @@ dwarf_begin_elf (Elf *elf, Dwarf_Cmd cmd, Elf_Scn *scngrp) assert (sizeof (struct Dwarf) < mem_default_size); /* Allocate the data structure. */ - Dwarf *result = (Dwarf *) calloc (1, sizeof (Dwarf)); + Dwarf *result = calloc (1, sizeof (Dwarf)); if (unlikely (result == NULL) || unlikely (Dwarf_Sig8_Hash_init (&result->sig8_hash, 11) < 0)) { |
