ELF: Don't create sections for section header index 0
authorPavel Labath <pavel@labath.sk>
Tue, 18 Dec 2018 15:56:45 +0000 (15:56 +0000)
committerPavel Labath <pavel@labath.sk>
Tue, 18 Dec 2018 15:56:45 +0000 (15:56 +0000)
Summary:
The first section header does not define a real section. Instead it is
used for various elf extensions. This patch skips creation of a section
for index 0.

This has one furtunate side-effect, in that it allows us to use the section
header index as the Section ID (where 0 is also invalid). This way, we
can get rid of a lot of spurious +1s in the ObjectFileELF code.

Reviewers: clayborg, krytarowski, joerg, espindola

Subscribers: emaste, lldb-commits, arichardson

Differential Revision: https://reviews.llvm.org/D55757

llvm-svn: 349498

lldb/lit/Modules/ELF/many-sections.s
lldb/lit/Modules/MachO/subsections.yaml
lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.h
lldb/tools/lldb-test/lldb-test.cpp

index a5e4aee188215cdc08c488f625b787d6bd3d9a17..15d7e8fc15893e6b15b10cb1685517ec8af8a197 100644 (file)
@@ -7,7 +7,7 @@
 ## aaaaaaaa..dddddddd, plus a couple of standard ones (.strtab, etc.)
 ## Check the number is correct plus the names of a couple of chosen sections.
 
-# CHECK: Showing 65541 sections
+# CHECK: Showing 65540 sections
 # CHECK: Name: aaaaaaaa
 # CHECK: Name: bbbbbbbb
 # CHECK: Name: cccccccc
index 7509b78ae00d0f15f8d34b3a95fa64e087de78ab..a92ef41d79fc0ff5de7ab8913ff73831227c1ce9 100644 (file)
@@ -3,6 +3,7 @@
 
 #CHECK:     Showing 2 sections
 #CHECK-NEXT:  Index: 0
+#CHECK-NEXT:  ID: 0x100
 #CHECK-NEXT:  Name: __PAGEZERO
 #CHECK-NEXT:  Type: container
 #CHECK-NEXT:  Permissions: ---
@@ -13,6 +14,7 @@
 #CHECK-NEXT:  There are no subsections
 #CHECK-EMPTY:
 #CHECK-NEXT:  Index: 1
+#CHECK-NEXT:  ID: 0x200
 #CHECK-NEXT:  Name: __TEXT
 #CHECK-NEXT:  Type: container
 #CHECK-NEXT:  Permissions: r-x
@@ -22,6 +24,7 @@
 #CHECK-NEXT:  File size: 4096
 #CHECK-NEXT:  Showing 3 subsections
 #CHECK-NEXT:    Index: 0
+#CHECK-NEXT:    ID: 0x1
 #CHECK-NEXT:    Name: __text
 #CHECK-NEXT:    Type: code
 #CHECK-NEXT:    Permissions: r-x
@@ -31,6 +34,7 @@
 #CHECK-NEXT:    File size: 22
 #CHECK-EMPTY:
 #CHECK-NEXT:    Index: 1
+#CHECK-NEXT:    ID: 0x2
 #CHECK-NEXT:    Name: __unwind_info
 #CHECK-NEXT:    Type: compact-unwind
 #CHECK-NEXT:    Permissions: r-x
@@ -40,6 +44,7 @@
 #CHECK-NEXT:    File size: 76
 #CHECK-EMPTY:
 #CHECK-NEXT:    Index: 2
+#CHECK-NEXT:    ID: 0x3
 #CHECK-NEXT:    Name: __eh_frame
 #CHECK-NEXT:    Type: eh-frame
 #CHECK-NEXT:    Permissions: r-x
index 601c35b1832b698f8f7d3f4cf0764fae7ae7462d..95685c23ec115bf033f17668438ee5464a3bc51d 100644 (file)
@@ -889,11 +889,11 @@ AddressClass ObjectFileELF::GetAddressClass(addr_t file_addr) {
 }
 
 size_t ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I) {
-  return std::distance(m_section_headers.begin(), I) + 1u;
+  return std::distance(m_section_headers.begin(), I);
 }
 
 size_t ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const {
-  return std::distance(m_section_headers.begin(), I) + 1u;
+  return std::distance(m_section_headers.begin(), I);
 }
 
 bool ObjectFileELF::ParseHeader() {
@@ -1081,7 +1081,7 @@ size_t ObjectFileELF::ParseDependentModules() {
     return 0;
   // sh_link: section header index of string table used by entries in the
   // section.
-  Section *dynstr = section_list->FindSectionByID(header->sh_link + 1).get();
+  Section *dynstr = section_list->FindSectionByID(header->sh_link).get();
   if (!dynstr)
     return 0;
 
@@ -1717,10 +1717,10 @@ size_t ObjectFileELF::ParseSectionHeaders() {
 
 const ObjectFileELF::ELFSectionHeaderInfo *
 ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id) {
-  if (!id || !ParseSectionHeaders())
+  if (!ParseSectionHeaders())
     return NULL;
 
-  if (--id < m_section_headers.size())
+  if (id < m_section_headers.size())
     return &m_section_headers[id];
 
   return NULL;
@@ -1853,7 +1853,7 @@ void ObjectFileELF::CreateSections(SectionList &unified_section_list) {
     m_sections_ap.reset(new SectionList());
 
     VMAddressProvider address_provider(CalculateType());
-    for (SectionHeaderCollIter I = m_section_headers.begin();
+    for (SectionHeaderCollIter I = std::next(m_section_headers.begin());
          I != m_section_headers.end(); ++I) {
       const ELFSectionHeaderInfo &header = *I;
 
@@ -1990,9 +1990,9 @@ unsigned ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id,
 
     SectionSP symbol_section_sp;
     SymbolType symbol_type = eSymbolTypeInvalid;
-    Elf64_Half section_idx = symbol.st_shndx;
+    Elf64_Half shndx = symbol.st_shndx;
 
-    switch (section_idx) {
+    switch (shndx) {
     case SHN_ABS:
       symbol_type = eSymbolTypeAbsolute;
       break;
@@ -2000,7 +2000,7 @@ unsigned ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id,
       symbol_type = eSymbolTypeUndefined;
       break;
     default:
-      symbol_section_sp = section_list->GetSectionAtIndex(section_idx);
+      symbol_section_sp = section_list->FindSectionByID(shndx);
       break;
     }
 
@@ -2169,7 +2169,7 @@ unsigned ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id,
     // symbols. See above for more details.
     uint64_t symbol_value = symbol.st_value + symbol_value_offset;
 
-    if (symbol_section_sp == nullptr && section_idx == SHN_ABS &&
+    if (symbol_section_sp == nullptr && shndx == SHN_ABS &&
         symbol.st_size != 0) {
       // We don't have a section for a symbol with non-zero size. Create a new
       // section for it so the address range covered by the symbol is also
@@ -2282,9 +2282,8 @@ unsigned ObjectFileELF::ParseSymbolTable(Symtab *symbol_table,
   assert(symtab_hdr->sh_type == SHT_SYMTAB ||
          symtab_hdr->sh_type == SHT_DYNSYM);
 
-  // sh_link: section header index of associated string table. Section ID's are
-  // ones based.
-  user_id_t strtab_id = symtab_hdr->sh_link + 1;
+  // sh_link: section header index of associated string table.
+  user_id_t strtab_id = symtab_hdr->sh_link;
   Section *strtab = section_list->FindSectionByID(strtab_id).get();
 
   if (symtab && strtab) {
@@ -2494,10 +2493,6 @@ ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table, user_id_t start_id,
   if (!symtab_id || !plt_id)
     return 0;
 
-  // Section ID's are ones based;
-  symtab_id++;
-  plt_id++;
-
   const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id);
   if (!plt_hdr)
     return 0;
@@ -2523,7 +2518,7 @@ ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table, user_id_t start_id,
     return 0;
 
   // sh_link points to associated string table.
-  Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get();
+  Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link).get();
   if (!strtab)
     return 0;
 
@@ -2651,9 +2646,8 @@ unsigned ObjectFileELF::RelocateDebugSections(const ELFSectionHeader *rel_hdr,
   if (!section_list)
     return 0;
 
-  // Section ID's are ones based.
-  user_id_t symtab_id = rel_hdr->sh_link + 1;
-  user_id_t debug_id = rel_hdr->sh_info + 1;
+  user_id_t symtab_id = rel_hdr->sh_link;
+  user_id_t debug_id = rel_hdr->sh_info;
 
   const ELFSectionHeader *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
   if (!symtab_hdr)
index 0ad4f429723e1a8e39e94c07139759c556633eaf..2e6478a82449ddb81d1e46ca81d83d7276d4d391 100644 (file)
@@ -220,10 +220,10 @@ private:
   /// The address class for each symbol in the elf file
   FileAddressToAddressClassMap m_address_class_map;
 
-  /// Returns a 1 based index of the given section header.
+  /// Returns the index of the given section header.
   size_t SectionIndex(const SectionHeaderCollIter &I);
 
-  /// Returns a 1 based index of the given section header.
+  /// Returns the index of the given section header.
   size_t SectionIndex(const SectionHeaderCollConstIter &I) const;
 
   // Parses the ELF program headers.
index eee664b09c9bde0bb33e8abab07cee95b62ea94b..d8f4472e2d06662a245c00226c089b03e453584a 100644 (file)
@@ -732,6 +732,7 @@ static void dumpSectionList(LinePrinter &Printer, const SectionList &List, bool
     assert(S);
     AutoIndent Indent(Printer, 2);
     Printer.formatLine("Index: {0}", I);
+    Printer.formatLine("ID: {0:x}", S->GetID());
     Printer.formatLine("Name: {0}", S->GetName().GetStringRef());
     Printer.formatLine("Type: {0}", S->GetTypeAsCString());
     Printer.formatLine("Permissions: {0}", GetPermissionsAsCString(S->GetPermissions()));