llvm.org GIT mirror llvm / fe57e34
[yaml2obj] Add support for sh_link via `Link` key. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@184022 91177308-0d34-0410-b5e6-96231b3b80d8 Sean Silva 7 years ago
4 changed file(s) with 49 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
5454 ELF_SHF Flags;
5555 llvm::yaml::Hex64 Address;
5656 object::yaml::BinaryRef Content;
57 StringRef Link;
5758 llvm::yaml::Hex64 AddressAlign;
5859 };
5960 struct Object {
266266 IO.mapOptional("Flags", Section.Flags, ELFYAML::ELF_SHF(0));
267267 IO.mapOptional("Address", Section.Address, Hex64(0));
268268 IO.mapOptional("Content", Section.Content);
269 IO.mapOptional("Link", Section.Link);
269270 IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0));
270271 }
271272
99 Type: SHT_PROGBITS
1010 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
1111 Address: 0xCAFEBABE
12 Link: .text # Doesn't make sense for SHT_PROGBITS, but good enough for test.
1213 Content: EBFE
1314 AddressAlign: 2
1415
2526 # CHECK-NEXT: ]
2627 # CHECK-NEXT: Address: 0xCAFEBABE
2728 # CHECK: Size: 2
29 # Check that Link != 0.
30 # CHECK: Link: {{[1-9][0-9]*}}
2831 # CHECK: AddressAlignment: 2
2932 # CHECK: SectionData (
3033 # CHECK-NEXT: 0000: EBFE
7373 raw_ostream &getOS() { return OS; }
7474 uint64_t currentOffset() const { return InitialOffset + OS.tell(); }
7575 void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); }
76 };
77
78 // Used to keep track of section names, so that in the YAML file sections
79 // can be referenced by name instead of by index.
80 class SectionNameToIdxMap {
81 StringMap Map;
82 public:
83 /// \returns true if name is already present in the map.
84 bool addName(StringRef SecName, unsigned i) {
85 StringMapEntry &Entry = Map.GetOrCreateValue(SecName, -1);
86 if (Entry.getValue() != -1)
87 return true;
88 Entry.setValue((int)i);
89 return false;
90 }
91 /// \returns true if name is not present in the map
92 bool lookupSection(StringRef SecName, unsigned &Idx) const {
93 StringMap::const_iterator I = Map.find(SecName);
94 if (I == Map.end())
95 return true;
96 Idx = I->getValue();
97 return false;
98 }
7699 };
77100
78101 template
137160 // Place section header string table last.
138161 Header.e_shstrndx = Sections.size();
139162
163 SectionNameToIdxMap SN2I;
164 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
165 StringRef Name = Sections[i].Name;
166 if (Name.empty())
167 continue;
168 if (SN2I.addName(Name, i)) {
169 errs() << "error: Repeated section name: '" << Name
170 << "' at YAML section number " << i << ".\n";
171 return;
172 }
173 }
174
140175 StringTableBuilder StrTab;
141176 SmallVector Buf;
142177 // XXX: This offset is tightly coupled with the order that we write
158193 SHeader.sh_size = Sec.Content.binary_size();
159194 Sec.Content.writeAsBinary(CBA.getOS());
160195
161 SHeader.sh_link = 0;
196 if (!Sec.Link.empty()) {
197 unsigned Index;
198 if (SN2I.lookupSection(Sec.Link, Index)) {
199 errs() << "error: Unknown section referenced: '" << Sec.Link
200 << "' at YAML section number " << i << ".\n";
201 return;
202 }
203 SHeader.sh_link = Index;
204 }
162205 SHeader.sh_info = 0;
163206 SHeader.sh_addralign = Sec.AddressAlign;
164207 SHeader.sh_entsize = 0;