@@ -868,6 +868,23 @@ static size_t countSkippableZeroBytes(ArrayRef<uint8_t> Buf) {
868868 return N & ~0x3 ;
869869}
870870
871+ // Returns a map from sections to their relocations.
872+ static std::map<SectionRef, std::vector<RelocationRef>>
873+ getRelocsMap (llvm::object::ObjectFile const &Obj) {
874+ std::map<SectionRef, std::vector<RelocationRef>> Ret;
875+ for (const SectionRef &Section : ToolSectionFilter (Obj)) {
876+ section_iterator RelSec = Section.getRelocatedSection ();
877+ if (RelSec == Obj.section_end ())
878+ continue ;
879+ std::vector<RelocationRef> &V = Ret[*RelSec];
880+ for (const RelocationRef &R : Section.relocations ())
881+ V.push_back (R);
882+ // Sort relocations by address.
883+ llvm::sort (V, isRelocAddressLess);
884+ }
885+ return Ret;
886+ }
887+
871888static void disassembleObject (const ObjectFile *Obj, bool InlineRelocs) {
872889 if (StartAddress > StopAddress)
873890 error (" Start address should be less than stop address" );
@@ -929,15 +946,9 @@ static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
929946
930947 SourcePrinter SP (Obj, TheTarget->getName ());
931948
932- // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
933- // in RelocSecs contain the relocations for section S.
934- std::error_code EC;
935- std::map<SectionRef, SmallVector<SectionRef, 1 >> SectionRelocMap;
936- for (const SectionRef &Section : ToolSectionFilter (*Obj)) {
937- section_iterator Sec2 = Section.getRelocatedSection ();
938- if (Sec2 != Obj->section_end ())
939- SectionRelocMap[*Sec2].push_back (Section);
940- }
949+ std::map<SectionRef, std::vector<RelocationRef>> RelocMap;
950+ if (InlineRelocs)
951+ RelocMap = getRelocsMap (*Obj);
941952
942953 // Create a mapping from virtual address to symbol name. This is used to
943954 // pretty print the symbols while disassembling.
@@ -1062,19 +1073,6 @@ static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
10621073 }
10631074 }
10641075
1065- // Make a list of all the relocations for this section.
1066- std::vector<RelocationRef> Rels;
1067- if (InlineRelocs) {
1068- for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
1069- for (const RelocationRef &Reloc : RelocSec.relocations ()) {
1070- Rels.push_back (Reloc);
1071- }
1072- }
1073- }
1074-
1075- // Sort relocations by address.
1076- llvm::sort (Rels, isRelocAddressLess);
1077-
10781076 StringRef SegmentName = " " ;
10791077 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
10801078 DataRefImpl DR = Section.getRawDataRefImpl ();
@@ -1103,6 +1101,7 @@ static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
11031101 uint64_t Index;
11041102 bool PrintedSection = false ;
11051103
1104+ std::vector<RelocationRef> Rels = RelocMap[Section];
11061105 std::vector<RelocationRef>::const_iterator RelCur = Rels.begin ();
11071106 std::vector<RelocationRef>::const_iterator RelEnd = Rels.end ();
11081107 // Disassemble symbol by symbol.
0 commit comments