llvm.org GIT mirror llvm / 942eb00
llvm-object: Add inline relocation information to disassembly. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141897 91177308-0d34-0410-b5e6-96231b3b80d8 Michael J. Spencer 8 years ago
2 changed file(s) with 80 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
0 RUN: llvm-objdump -d -r %p/TestObjectFiles/trivial-object-test.coff-i386 \
1 RUN: | FileCheck %s -check-prefix COFF-i386
2 RUN: llvm-objdump -d -r %p/TestObjectFiles/trivial-object-test.coff-x86-64 \
3 RUN: | FileCheck %s -check-prefix COFF-x86-64
4
5 COFF-i386: file format COFF-i386
6 COFF-i386: Disassembly of section .text:
7 COFF-i386: 0: 83 ec 0c subl $12, %esp
8 COFF-i386: 3: c7 44 24 08 00 00 00 00 movl $0, 8(%esp)
9 COFF-i386: b: c7 04 24 00 00 00 00 movl $0, (%esp)
10 COFF-i386: e: IMAGE_REL_I386_DIR32 L_.str
11 COFF-i386: 12: e8 00 00 00 00 calll 0
12 COFF-i386: 13: IMAGE_REL_I386_REL32 _puts
13 COFF-i386: 17: e8 00 00 00 00 calll 0
14 COFF-i386: 18: IMAGE_REL_I386_REL32 _SomeOtherFunction
15 COFF-i386: 1c: 8b 44 24 08 movl 8(%esp), %eax
16 COFF-i386: 20: 83 c4 0c addl $12, %esp
17 COFF-i386: 23: c3 ret
18
19 COFF-x86-64: file format COFF-x86-64
20 COFF-x86-64: Disassembly of section .text:
21 COFF-x86-64: 0: 48 83 ec 28 subq $40, %rsp
22 COFF-x86-64: 4: c7 44 24 24 00 00 00 00 movl $0, 36(%rsp)
23 COFF-x86-64: c: 48 8d 0d 00 00 00 00 leaq (%rip), %rcx
24 COFF-x86-64: f: IMAGE_REL_AMD64_REL32 L.str
25 COFF-x86-64: 13: e8 00 00 00 00 callq 0
26 COFF-x86-64: 14: IMAGE_REL_AMD64_REL32 puts
27 COFF-x86-64: 18: e8 00 00 00 00 callq 0
28 COFF-x86-64: 19: IMAGE_REL_AMD64_REL32 SomeOtherFunction
29 COFF-x86-64: 1d: 8b 44 24 24 movl 36(%rsp), %eax
30 COFF-x86-64: 21: 48 83 c4 28 addq $40, %rsp
31 COFF-x86-64: 25: c3 ret
143143 outs() << output;
144144 }
145145
146 static void DisassembleObject(const ObjectFile *Obj) {
146 static bool RelocAddressLess(RelocationRef a, RelocationRef b) {
147 uint64_t a_addr, b_addr;
148 if (error(a.getAddress(a_addr))) return false;
149 if (error(b.getAddress(b_addr))) return false;
150 return a_addr < b_addr;
151 }
152
153 static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
147154 const Target *TheTarget = GetTarget(Obj);
148155 if (!TheTarget) {
149156 // GetTarget prints out stuff.
162169 bool text;
163170 if (error(i->isText(text))) break;
164171 if (!text) continue;
172
173 uint64_t SectionAddr;
174 if (error(i->getAddress(SectionAddr))) break;
165175
166176 // Make a list of all the symbols in this section.
167177 std::vector > Symbols;
181191 // Sort the symbols by address, just in case they didn't come in that way.
182192 array_pod_sort(Symbols.begin(), Symbols.end());
183193
194 // Make a list of all the relocations for this section.
195 std::vector Rels;
196 if (InlineRelocs) {
197 for (relocation_iterator ri = i->begin_relocations(),
198 re = i->end_relocations();
199 ri != re; ri.increment(ec)) {
200 if (error(ec)) break;
201 Rels.push_back(*ri);
202 }
203 }
204
205 // Sort relocations by address.
206 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
207
184208 StringRef name;
185209 if (error(i->getName(name))) break;
186210 outs() << "Disassembly of section " << name << ':';
230254 uint64_t SectSize;
231255 if (error(i->getSize(SectSize))) break;
232256
257 std::vector::const_iterator rel_cur = Rels.begin();
258 std::vector::const_iterator rel_end = Rels.end();
233259 // Disassemble symbol by symbol.
234260 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
235261 uint64_t Start = Symbols[si].first;
258284
259285 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
260286 DebugOut, nulls())) {
261 uint64_t addr;
262 if (error(i->getAddress(addr))) break;
263 outs() << format("%8x:\t", addr + Index);
287 outs() << format("%8x:\t", SectionAddr + Index);
264288 DumpBytes(StringRef(Bytes.data() + Index, Size));
265289 IP->printInst(&Inst, outs(), "");
266290 outs() << "\n";
268292 errs() << ToolName << ": warning: invalid instruction encoding\n";
269293 if (Size == 0)
270294 Size = 1; // skip illegible bytes
295 }
296
297 // Print relocation for instruction.
298 while (rel_cur != rel_end) {
299 uint64_t addr;
300 SmallString<16> name;
301 SmallString<32> val;
302 if (error(rel_cur->getAddress(addr))) goto skip_print_rel;
303 // Stop when rel_cur's address is past the current instruction.
304 if (addr > Index + Size) break;
305 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
306 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
307
308 outs() << format("\t\t\t%8x: ", SectionAddr + addr) << name << "\t"
309 << val << "\n";
310
311 skip_print_rel:
312 ++rel_cur;
271313 }
272314 }
273315 }
329371
330372 static void DumpObject(const ObjectFile *o) {
331373 if (Disassemble)
332 DisassembleObject(o);
333 if (Relocations)
374 DisassembleObject(o, Relocations);
375 if (Relocations && !Disassemble)
334376 PrintRelocations(o);
335377 if (SectionHeaders)
336378 PrintSectionHeaders(o);