llvm.org GIT mirror llvm / 7a2da95
[C++11] Use ObjectFile::sections() in commandline llvm tools git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203802 91177308-0d34-0410-b5e6-96231b3b80d8 Alexey Samsonov 5 years ago
5 changed file(s) with 114 addition(s) and 117 deletion(s). Raw diff Collapse all Expand all
157157 SI != SE; ++SI)
158158 Symbols.push_back(*SI);
159159
160 for (section_iterator SI = MachOObj->section_begin(),
161 SE = MachOObj->section_end();
162 SI != SE; ++SI) {
163 SectionRef SR = *SI;
160 for (const SectionRef &Section : MachOObj->sections()) {
164161 StringRef SectName;
165 SR.getName(SectName);
166 Sections.push_back(*SI);
162 Section.getName(SectName);
163 Sections.push_back(Section);
167164 }
168165
169166 MachOObjectFile::LoadCommandInfo Command =
170 MachOObj->getFirstLoadCommandInfo();
167 MachOObj->getFirstLoadCommandInfo();
171168 bool BaseSegmentAddressSet = false;
172169 for (unsigned i = 0; ; ++i) {
173170 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
384384 // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
385385 // in RelocSecs contain the relocations for section S.
386386 error_code EC;
387 std::map > SectionRelocMap;
388 for (section_iterator I = Obj->section_begin(), E = Obj->section_end();
389 I != E; ++I) {
390 section_iterator Sec2 = I->getRelocatedSection();
387 std::map> SectionRelocMap;
388 for (const SectionRef &Section : Obj->sections()) {
389 section_iterator Sec2 = Section.getRelocatedSection();
391390 if (Sec2 != Obj->section_end())
392 SectionRelocMap[*Sec2].push_back(*I);
393 }
394
395 for (section_iterator I = Obj->section_begin(), E = Obj->section_end();
396 I != E; ++I) {
391 SectionRelocMap[*Sec2].push_back(Section);
392 }
393
394 for (const SectionRef &Section : Obj->sections()) {
397395 bool Text;
398 if (error(I->isText(Text)))
396 if (error(Section.isText(Text)))
399397 break;
400398 if (!Text)
401399 continue;
402400
403401 uint64_t SectionAddr;
404 if (error(I->getAddress(SectionAddr)))
402 if (error(Section.getAddress(SectionAddr)))
405403 break;
406404
407405 uint64_t SectSize;
408 if (error(I->getSize(SectSize)))
406 if (error(Section.getSize(SectSize)))
409407 break;
410408
411409 // Make a list of all the symbols in this section.
413411 for (symbol_iterator SI = Obj->symbol_begin(), SE = Obj->symbol_end();
414412 SI != SE; ++SI) {
415413 bool contains;
416 if (!error(I->containsSymbol(*SI, contains)) && contains) {
414 if (!error(Section.containsSymbol(*SI, contains)) && contains) {
417415 uint64_t Address;
418416 if (error(SI->getAddress(Address)))
419417 break;
436434 // Make a list of all the relocations for this section.
437435 std::vector Rels;
438436 if (InlineRelocs) {
439 SmallVectorImpl *RelocSecs = &SectionRelocMap[*I];
437 SmallVectorImpl *RelocSecs = &SectionRelocMap[Section];
440438 for (SmallVectorImpl::iterator RelocSec = RelocSecs->begin(),
441439 E = RelocSecs->end();
442440 RelocSec != E; ++RelocSec) {
452450
453451 StringRef SegmentName = "";
454452 if (const MachOObjectFile *MachO = dyn_cast(Obj)) {
455 DataRefImpl DR = I->getRawDataRefImpl();
453 DataRefImpl DR = Section.getRawDataRefImpl();
456454 SegmentName = MachO->getSectionFinalSegmentName(DR);
457455 }
458456 StringRef name;
459 if (error(I->getName(name)))
457 if (error(Section.getName(name)))
460458 break;
461459 outs() << "Disassembly of section ";
462460 if (!SegmentName.empty())
473471 raw_svector_ostream CommentStream(Comments);
474472
475473 StringRef Bytes;
476 if (error(I->getContents(Bytes)))
474 if (error(Section.getContents(Bytes)))
477475 break;
478476 StringRefMemoryObject memoryObject(Bytes, SectionAddr);
479477 uint64_t Size;
553551 }
554552 }
555553
556 static void PrintRelocations(const ObjectFile *o) {
557 for (section_iterator si = o->section_begin(), se = o->section_end();
558 si != se; ++si) {
559 if (si->relocation_begin() == si->relocation_end())
554 static void PrintRelocations(const ObjectFile *Obj) {
555 for (const SectionRef &Section : Obj->sections()) {
556 if (Section.relocation_begin() == Section.relocation_end())
560557 continue;
561558 StringRef secname;
562 if (error(si->getName(secname))) continue;
559 if (error(Section.getName(secname)))
560 continue;
563561 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
564 for (relocation_iterator ri = si->relocation_begin(),
565 re = si->relocation_end();
562 for (relocation_iterator ri = Section.relocation_begin(),
563 re = Section.relocation_end();
566564 ri != re; ++ri) {
567565 bool hidden;
568566 uint64_t address;
579577 }
580578 }
581579
582 static void PrintSectionHeaders(const ObjectFile *o) {
580 static void PrintSectionHeaders(const ObjectFile *Obj) {
583581 outs() << "Sections:\n"
584582 "Idx Name Size Address Type\n";
585583 unsigned i = 0;
586 for (section_iterator si = o->section_begin(), se = o->section_end();
587 si != se; ++si) {
584 for (const SectionRef &Section : Obj->sections()) {
588585 StringRef Name;
589 if (error(si->getName(Name)))
586 if (error(Section.getName(Name)))
590587 return;
591588 uint64_t Address;
592 if (error(si->getAddress(Address))) return;
589 if (error(Section.getAddress(Address)))
590 return;
593591 uint64_t Size;
594 if (error(si->getSize(Size))) return;
592 if (error(Section.getSize(Size)))
593 return;
595594 bool Text, Data, BSS;
596 if (error(si->isText(Text))) return;
597 if (error(si->isData(Data))) return;
598 if (error(si->isBSS(BSS))) return;
595 if (error(Section.isText(Text)))
596 return;
597 if (error(Section.isData(Data)))
598 return;
599 if (error(Section.isBSS(BSS)))
600 return;
599601 std::string Type = (std::string(Text ? "TEXT " : "") +
600602 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
601 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n",
602 i, Name.str().c_str(), Size, Address, Type.c_str());
603 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
604 Name.str().c_str(), Size, Address, Type.c_str());
603605 ++i;
604606 }
605607 }
606608
607 static void PrintSectionContents(const ObjectFile *o) {
609 static void PrintSectionContents(const ObjectFile *Obj) {
608610 error_code EC;
609 for (section_iterator si = o->section_begin(), se = o->section_end();
610 si != se; ++si) {
611 for (const SectionRef &Section : Obj->sections()) {
611612 StringRef Name;
612613 StringRef Contents;
613614 uint64_t BaseAddr;
614615 bool BSS;
615 if (error(si->getName(Name))) continue;
616 if (error(si->getContents(Contents))) continue;
617 if (error(si->getAddress(BaseAddr))) continue;
618 if (error(si->isBSS(BSS))) continue;
616 if (error(Section.getName(Name)))
617 continue;
618 if (error(Section.getContents(Contents)))
619 continue;
620 if (error(Section.getAddress(BaseAddr)))
621 continue;
622 if (error(Section.isBSS(BSS)))
623 continue;
619624
620625 outs() << "Contents of section " << Name << ":\n";
621626 if (BSS) {
3939 private:
4040 void printSymbol(symbol_iterator SymI);
4141
42 void printRelocation(section_iterator SecI, relocation_iterator RelI);
43
44 void printRelocation(const MachOObjectFile *Obj,
45 section_iterator SecI, relocation_iterator RelI);
42 void printRelocation(relocation_iterator RelI);
43
44 void printRelocation(const MachOObjectFile *Obj, relocation_iterator RelI);
4645
4746 void printSections(const MachOObjectFile *Obj);
4847
215214 ListScope Group(W, "Sections");
216215
217216 int SectionIndex = -1;
218 for (section_iterator SecI = Obj->section_begin(),
219 SecE = Obj->section_end();
220 SecI != SecE; ++SecI) {
217 for (const SectionRef &Section : Obj->sections()) {
221218 ++SectionIndex;
222219
223 MachOSection Section;
224 getSection(Obj, SecI->getRawDataRefImpl(), Section);
225 DataRefImpl DR = SecI->getRawDataRefImpl();
220 MachOSection MOSection;
221 getSection(Obj, Section.getRawDataRefImpl(), MOSection);
222 DataRefImpl DR = Section.getRawDataRefImpl();
226223
227224 StringRef Name;
228 if (error(SecI->getName(Name)))
229 Name = "";
225 if (error(Section.getName(Name)))
226 Name = "";
230227
231228 ArrayRef RawName = Obj->getSectionRawName(DR);
232229 StringRef SegmentName = Obj->getSectionFinalSegmentName(DR);
236233 W.printNumber("Index", SectionIndex);
237234 W.printBinary("Name", Name, RawName);
238235 W.printBinary("Segment", SegmentName, RawSegmentName);
239 W.printHex ("Address", Section.Address);
240 W.printHex ("Size", Section.Size);
241 W.printNumber("Offset", Section.Offset);
242 W.printNumber("Alignment", Section.Alignment);
243 W.printHex ("RelocationOffset", Section.RelocationTableOffset);
244 W.printNumber("RelocationCount", Section.NumRelocationTableEntries);
245 W.printEnum ("Type", Section.Flags & 0xFF,
246 makeArrayRef(MachOSectionAttributes));
247 W.printFlags ("Attributes", Section.Flags >> 8,
248 makeArrayRef(MachOSectionAttributes));
249 W.printHex ("Reserved1", Section.Reserved1);
250 W.printHex ("Reserved2", Section.Reserved2);
236 W.printHex("Address", MOSection.Address);
237 W.printHex("Size", MOSection.Size);
238 W.printNumber("Offset", MOSection.Offset);
239 W.printNumber("Alignment", MOSection.Alignment);
240 W.printHex("RelocationOffset", MOSection.RelocationTableOffset);
241 W.printNumber("RelocationCount", MOSection.NumRelocationTableEntries);
242 W.printEnum("Type", MOSection.Flags & 0xFF,
243 makeArrayRef(MachOSectionAttributes));
244 W.printFlags("Attributes", MOSection.Flags >> 8,
245 makeArrayRef(MachOSectionAttributes));
246 W.printHex("Reserved1", MOSection.Reserved1);
247 W.printHex("Reserved2", MOSection.Reserved2);
251248
252249 if (opts::SectionRelocations) {
253250 ListScope D(W, "Relocations");
254 for (relocation_iterator RelI = SecI->relocation_begin(),
255 RelE = SecI->relocation_end();
251 for (relocation_iterator RelI = Section.relocation_begin(),
252 RelE = Section.relocation_end();
256253 RelI != RelE; ++RelI)
257 printRelocation(SecI, RelI);
254 printRelocation(RelI);
258255 }
259256
260257 if (opts::SectionSymbols) {
261258 ListScope D(W, "Symbols");
262 for (symbol_iterator SymI = Obj->symbol_begin(),
263 SymE = Obj->symbol_end();
259 for (symbol_iterator SymI = Obj->symbol_begin(), SymE = Obj->symbol_end();
264260 SymI != SymE; ++SymI) {
265261 bool Contained = false;
266 if (SecI->containsSymbol(*SymI, Contained) || !Contained)
262 if (Section.containsSymbol(*SymI, Contained) || !Contained)
267263 continue;
268264
269265 printSymbol(SymI);
272268
273269 if (opts::SectionData) {
274270 StringRef Data;
275 if (error(SecI->getContents(Data))) break;
271 if (error(Section.getContents(Data)))
272 break;
276273
277274 W.printBinaryBlock("SectionData", Data);
278275 }
283280 ListScope D(W, "Relocations");
284281
285282 error_code EC;
286 for (section_iterator SecI = Obj->section_begin(),
287 SecE = Obj->section_end();
288 SecI != SecE; ++SecI) {
283 for (const SectionRef &Section : Obj->sections()) {
289284 StringRef Name;
290 if (error(SecI->getName(Name)))
285 if (error(Section.getName(Name)))
291286 continue;
292287
293288 bool PrintedGroup = false;
294 for (relocation_iterator RelI = SecI->relocation_begin(),
295 RelE = SecI->relocation_end();
289 for (relocation_iterator RelI = Section.relocation_begin(),
290 RelE = Section.relocation_end();
296291 RelI != RelE; ++RelI) {
297292 if (!PrintedGroup) {
298293 W.startLine() << "Section " << Name << " {\n";
300295 PrintedGroup = true;
301296 }
302297
303 printRelocation(SecI, RelI);
298 printRelocation(RelI);
304299 }
305300
306301 if (PrintedGroup) {
310305 }
311306 }
312307
313 void MachODumper::printRelocation(section_iterator SecI,
314 relocation_iterator RelI) {
315 return printRelocation(Obj, SecI, RelI);
308 void MachODumper::printRelocation(relocation_iterator RelI) {
309 return printRelocation(Obj, RelI);
316310 }
317311
318312 void MachODumper::printRelocation(const MachOObjectFile *Obj,
319 section_iterator SecI,
320313 relocation_iterator RelI) {
321314 uint64_t Offset;
322315 SmallString<32> RelocName;
8484 return result.size();
8585 }
8686
87 /// @brief Print the size of each section in @p o.
87 /// @brief Print the size of each section in @p Obj.
8888 ///
8989 /// The format used is determined by @c OutputFormat and @c Radix.
90 static void PrintObjectSectionSizes(ObjectFile *o) {
90 static void PrintObjectSectionSizes(ObjectFile *Obj) {
9191 uint64_t total = 0;
9292 std::string fmtbuf;
9393 raw_string_ostream fmt(fmtbuf);
110110 std::size_t max_name_len = strlen("section");
111111 std::size_t max_size_len = strlen("size");
112112 std::size_t max_addr_len = strlen("addr");
113 for (section_iterator i = o->section_begin(), e = o->section_end();
114 i != e; ++i) {
113 for (const SectionRef &Section : Obj->sections()) {
115114 uint64_t size = 0;
116 if (error(i->getSize(size)))
115 if (error(Section.getSize(size)))
117116 return;
118117 total += size;
119118
120119 StringRef name;
121120 uint64_t addr = 0;
122 if (error(i->getName(name))) return;
123 if (error(i->getAddress(addr))) return;
121 if (error(Section.getName(name)))
122 return;
123 if (error(Section.getAddress(addr)))
124 return;
124125 max_name_len = std::max(max_name_len, name.size());
125126 max_size_len = std::max(max_size_len, getNumLengthAsString(size));
126127 max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr));
149150 << "%#" << max_addr_len << radix_fmt << "\n";
150151
151152 // Print each section.
152 for (section_iterator i = o->section_begin(), e = o->section_end();
153 i != e; ++i) {
153 for (const SectionRef &Section : Obj->sections()) {
154154 StringRef name;
155155 uint64_t size = 0;
156156 uint64_t addr = 0;
157 if (error(i->getName(name))) return;
158 if (error(i->getSize(size))) return;
159 if (error(i->getAddress(addr))) return;
157 if (error(Section.getName(name)))
158 return;
159 if (error(Section.getSize(size)))
160 return;
161 if (error(Section.getAddress(addr)))
162 return;
160163 std::string namestr = name;
161164
162 outs() << format(fmt.str().c_str(),
163 namestr.c_str(),
164 size,
165 addr);
165 outs() << format(fmt.str().c_str(), namestr.c_str(), size, addr);
166166 }
167167
168168 // Print total.
180180 uint64_t total_bss = 0;
181181
182182 // Make one pass over the section table to calculate sizes.
183 for (section_iterator i = o->section_begin(), e = o->section_end();
184 i != e; ++i) {
183 for (const SectionRef &Section : Obj->sections()) {
185184 uint64_t size = 0;
186185 bool isText = false;
187186 bool isData = false;
188187 bool isBSS = false;
189 if (error(i->getSize(size))) return;
190 if (error(i->isText(isText))) return;
191 if (error(i->isData(isData))) return;
192 if (error(i->isBSS(isBSS))) return;
188 if (error(Section.getSize(size)))
189 return;
190 if (error(Section.isText(isText)))
191 return;
192 if (error(Section.isData(isData)))
193 return;
194 if (error(Section.isBSS(isBSS)))
195 return;
193196 if (isText)
194197 total_text += size;
195198 else if (isData)
278278 const ObjectFile *Obj = dyn_cast(Bin);
279279 if (!Obj)
280280 return false;
281 for (section_iterator I = Obj->section_begin(), E = Obj->section_end();
282 I != E; ++I) {
281 for (const SectionRef &Section : Obj->sections()) {
283282 StringRef Name;
284 I->getName(Name);
283 Section.getName(Name);
285284 Name = Name.substr(Name.find_first_not_of("._"));
286285 if (Name == "gnu_debuglink") {
287286 StringRef Data;
288 I->getContents(Data);
287 Section.getContents(Data);
289288 DataExtractor DE(Data, Obj->isLittleEndian(), 0);
290289 uint32_t Offset = 0;
291290 if (const char *DebugNameStr = DE.getCStr(&Offset)) {