llvm.org GIT mirror llvm / 38ac43b
[yaml2obj][ELF] Move section index to the ELFState class. No functional changes. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@205432 91177308-0d34-0410-b5e6-96231b3b80d8 Simon Atanasyan 5 years ago
1 changed file(s) with 40 addition(s) and 33 deletion(s). Raw diff Collapse all Expand all
153153 class ELFState {
154154 /// \brief The future ".strtab" section.
155155 StringTableBuilder DotStrtab;
156 /// \brief The section number of the ".strtab" section.
157 unsigned DotStrtabSecNo;
158156 typedef typename object::ELFFile::Elf_Ehdr Elf_Ehdr;
159157
160 SectionNameToIdxMap &SN2I;
158 SectionNameToIdxMap SN2I;
159 const ELFYAML::Object &Doc;
161160
162161 public:
163 ELFState(unsigned DotStrtabSecNo_, SectionNameToIdxMap &SN2I_)
164 : DotStrtab(), DotStrtabSecNo(DotStrtabSecNo_), SN2I(SN2I_) {}
165
166 unsigned getDotStrTabSecNo() const { return DotStrtabSecNo; }
162 ELFState(const ELFYAML::Object &D) : Doc(D) {}
163
164 // - SHT_NULL entry (placed first, i.e. 0'th entry)
165 // - symbol table (.symtab) (placed third to last)
166 // - string table (.strtab) (placed second to last)
167 // - section header string table (.shstrtab) (placed last)
168 unsigned getDotSymTabSecNo() const { return Doc.Sections.size() + 1; }
169 unsigned getDotStrTabSecNo() const { return Doc.Sections.size() + 2; }
170 unsigned getDotShStrTabSecNo() const { return Doc.Sections.size() + 3; }
171 unsigned getSectionCount() const { return Doc.Sections.size() + 4; }
172
167173 StringTableBuilder &getStringTable() { return DotStrtab; }
168174 SectionNameToIdxMap &getSN2I() { return SN2I; }
175
176 bool buildSectionIndex() {
177 SN2I.addName(".symtab", getDotSymTabSecNo());
178 SN2I.addName(".strtab", getDotStrTabSecNo());
179 SN2I.addName(".shstrtab", getDotShStrTabSecNo());
180
181 for (unsigned i = 0, e = Doc.Sections.size(); i != e; ++i) {
182 StringRef Name = Doc.Sections[i].Name;
183 if (Name.empty())
184 continue;
185 // "+ 1" to take into account the SHT_NULL entry.
186 if (SN2I.addName(Name, i + 1)) {
187 errs() << "error: Repeated section name: '" << Name
188 << "' at YAML section number " << i << ".\n";
189 return false;
190 }
191 }
192 return true;
193 }
169194 };
170195 } // end anonymous namespace
171196
235260 typedef typename object::ELFFile::Elf_Ehdr Elf_Ehdr;
236261 typedef typename object::ELFFile::Elf_Shdr Elf_Shdr;
237262
263 ELFState State(Doc);
264 if (!State.buildSectionIndex())
265 return 1;
266
238267 const ELFYAML::FileHeader &Hdr = Doc.Header;
239268
240269 Elf_Ehdr Header;
262291 Header.e_shentsize = sizeof(Elf_Shdr);
263292 // Immediately following the ELF header.
264293 Header.e_shoff = sizeof(Header);
265 const std::vector &Sections = Doc.Sections;
266 // "+ 4" for
267 // - SHT_NULL entry (placed first, i.e. 0'th entry)
268 // - symbol table (.symtab) (placed third to last)
269 // - string table (.strtab) (placed second to last)
270 // - section header string table. (placed last)
271 Header.e_shnum = Sections.size() + 4;
272 // Place section header string table last.
273 Header.e_shstrndx = Header.e_shnum - 1;
274 const unsigned DotStrtabSecNo = Header.e_shnum - 2;
294 Header.e_shnum = State.getSectionCount();
295 Header.e_shstrndx = State.getDotShStrTabSecNo();
275296
276297 // XXX: This offset is tightly coupled with the order that we write
277298 // things to `OS`.
278299 const size_t SectionContentBeginOffset =
279300 Header.e_ehsize + Header.e_shentsize * Header.e_shnum;
280301 ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
281 SectionNameToIdxMap SN2I;
282 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
283 StringRef Name = Sections[i].Name;
284 if (Name.empty())
285 continue;
286 // "+ 1" to take into account the SHT_NULL entry.
287 if (SN2I.addName(Name, i + 1)) {
288 errs() << "error: Repeated section name: '" << Name
289 << "' at YAML section number " << i << ".\n";
290 return 1;
291 }
292 }
293
294 ELFState State(DotStrtabSecNo, SN2I);
295302
296303 StringTableBuilder SHStrTab;
297304 std::vector SHeaders;
302309 zero(SHdr);
303310 SHeaders.push_back(SHdr);
304311 }
305 for (const auto &Sec : Sections) {
312 for (const auto &Sec : Doc.Sections) {
306313 Elf_Shdr SHeader;
307314 zero(SHeader);
308315 SHeader.sh_name = SHStrTab.addString(Sec.Name);
315322
316323 if (!Sec.Link.empty()) {
317324 unsigned Index;
318 if (SN2I.lookupSection(Sec.Link, Index)) {
325 if (State.getSN2I().lookupSection(Sec.Link, Index)) {
319326 errs() << "error: Unknown section referenced: '" << Sec.Link
320327 << "' at YAML section '" << Sec.Name << "'.\n";
321328 return 1;