[llvm] r184022 - [yaml2obj] Add support for sh_link via `Link` key.

Sean Silva silvas at purdue.edu
Fri Jun 14 17:25:27 PDT 2013


Author: silvas
Date: Fri Jun 14 19:25:26 2013
New Revision: 184022

URL: http://llvm.org/viewvc/llvm-project?rev=184022&view=rev
Log:
[yaml2obj] Add support for sh_link via `Link` key.

Modified:
    llvm/trunk/include/llvm/Object/ELFYAML.h
    llvm/trunk/lib/Object/ELFYAML.cpp
    llvm/trunk/test/Object/yaml2obj-elf-section-basic.yaml
    llvm/trunk/tools/yaml2obj/yaml2elf.cpp

Modified: llvm/trunk/include/llvm/Object/ELFYAML.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ELFYAML.h?rev=184022&r1=184021&r2=184022&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ELFYAML.h (original)
+++ llvm/trunk/include/llvm/Object/ELFYAML.h Fri Jun 14 19:25:26 2013
@@ -55,6 +55,7 @@ struct Section {
   ELF_SHF Flags;
   llvm::yaml::Hex64 Address;
   object::yaml::BinaryRef Content;
+  StringRef Link;
   llvm::yaml::Hex64 AddressAlign;
 };
 struct Object {

Modified: llvm/trunk/lib/Object/ELFYAML.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/ELFYAML.cpp?rev=184022&r1=184021&r2=184022&view=diff
==============================================================================
--- llvm/trunk/lib/Object/ELFYAML.cpp (original)
+++ llvm/trunk/lib/Object/ELFYAML.cpp Fri Jun 14 19:25:26 2013
@@ -267,6 +267,7 @@ void MappingTraits<ELFYAML::Section>::ma
   IO.mapOptional("Flags", Section.Flags, ELFYAML::ELF_SHF(0));
   IO.mapOptional("Address", Section.Address, Hex64(0));
   IO.mapOptional("Content", Section.Content);
+  IO.mapOptional("Link", Section.Link);
   IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0));
 }
 

Modified: llvm/trunk/test/Object/yaml2obj-elf-section-basic.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/yaml2obj-elf-section-basic.yaml?rev=184022&r1=184021&r2=184022&view=diff
==============================================================================
--- llvm/trunk/test/Object/yaml2obj-elf-section-basic.yaml (original)
+++ llvm/trunk/test/Object/yaml2obj-elf-section-basic.yaml Fri Jun 14 19:25:26 2013
@@ -10,6 +10,7 @@ Sections:
     Type: SHT_PROGBITS
     Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
     Address: 0xCAFEBABE
+    Link: .text # Doesn't make sense for SHT_PROGBITS, but good enough for test.
     Content: EBFE
     AddressAlign: 2
 
@@ -26,6 +27,8 @@ Sections:
 # CHECK-NEXT:     ]
 # CHECK-NEXT:     Address: 0xCAFEBABE
 # CHECK:          Size: 2
+# Check that Link != 0.
+# CHECK:          Link: {{[1-9][0-9]*}}
 # CHECK:          AddressAlignment: 2
 # CHECK:          SectionData (
 # CHECK-NEXT:       0000: EBFE

Modified: llvm/trunk/tools/yaml2obj/yaml2elf.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/yaml2obj/yaml2elf.cpp?rev=184022&r1=184021&r2=184022&view=diff
==============================================================================
--- llvm/trunk/tools/yaml2obj/yaml2elf.cpp (original)
+++ llvm/trunk/tools/yaml2obj/yaml2elf.cpp Fri Jun 14 19:25:26 2013
@@ -76,6 +76,29 @@ public:
   void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); }
 };
 
+// Used to keep track of section names, so that in the YAML file sections
+// can be referenced by name instead of by index.
+class SectionNameToIdxMap {
+  StringMap<int> Map;
+public:
+  /// \returns true if name is already present in the map.
+  bool addName(StringRef SecName, unsigned i) {
+    StringMapEntry<int> &Entry = Map.GetOrCreateValue(SecName, -1);
+    if (Entry.getValue() != -1)
+      return true;
+    Entry.setValue((int)i);
+    return false;
+  }
+  /// \returns true if name is not present in the map
+  bool lookupSection(StringRef SecName, unsigned &Idx) const {
+    StringMap<int>::const_iterator I = Map.find(SecName);
+    if (I == Map.end())
+      return true;
+    Idx = I->getValue();
+    return false;
+  }
+};
+
 template <class T>
 static size_t vectorDataSize(const std::vector<T> &Vec) {
   return Vec.size() * sizeof(T);
@@ -138,6 +161,18 @@ static void writeELF(raw_ostream &OS, co
   // Place section header string table last.
   Header.e_shstrndx = Sections.size();
 
+  SectionNameToIdxMap SN2I;
+  for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
+    StringRef Name = Sections[i].Name;
+    if (Name.empty())
+      continue;
+    if (SN2I.addName(Name, i)) {
+      errs() << "error: Repeated section name: '" << Name
+             << "' at YAML section number " << i << ".\n";
+      return;
+    }
+  }
+
   StringTableBuilder StrTab;
   SmallVector<char, 128> Buf;
   // XXX: This offset is tightly coupled with the order that we write
@@ -159,7 +194,15 @@ static void writeELF(raw_ostream &OS, co
     SHeader.sh_size = Sec.Content.binary_size();
     Sec.Content.writeAsBinary(CBA.getOS());
 
-    SHeader.sh_link = 0;
+    if (!Sec.Link.empty()) {
+      unsigned Index;
+      if (SN2I.lookupSection(Sec.Link, Index)) {
+        errs() << "error: Unknown section referenced: '" << Sec.Link
+               << "' at YAML section number " << i << ".\n";
+        return;
+      }
+      SHeader.sh_link = Index;
+    }
     SHeader.sh_info = 0;
     SHeader.sh_addralign = Sec.AddressAlign;
     SHeader.sh_entsize = 0;





More information about the llvm-commits mailing list