llvm.org GIT mirror llvm / 023bb15
Add support for dumping section headers to llvm-objdump. This uses the same flags as binutils objdump but the output is different, not just in format but also showing different sections. Compare its results against readelf, not objdump. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141579 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Lewycky 9 years ago
3 changed file(s) with 117 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
125125 }
126126 };
127127
128 /// format_object4 - This is a templated helper class used by the format
129 /// function that captures the object to be formated and the format string. When
130 /// actually printed, this synthesizes the string into a temporary buffer
131 /// provided and returns whether or not it is big enough.
132 template
133 class format_object4 : public format_object_base {
134 T1 Val1;
135 T2 Val2;
136 T3 Val3;
137 T4 Val4;
138 public:
139 format_object4(const char *fmt, const T1 &val1, const T2 &val2,
140 const T3 &val3, const T4 &val4)
141 : format_object_base(fmt), Val1(val1), Val2(val2), Val3(val3), Val4(val4) {
142 }
143
144 virtual int snprint(char *Buffer, unsigned BufferSize) const {
145 return snprintf(Buffer, BufferSize, Fmt, Val1, Val2, Val3, Val4);
146 }
147 };
148
149 /// format_object5 - This is a templated helper class used by the format
150 /// function that captures the object to be formated and the format string. When
151 /// actually printed, this synthesizes the string into a temporary buffer
152 /// provided and returns whether or not it is big enough.
153 template
154 class format_object5 : public format_object_base {
155 T1 Val1;
156 T2 Val2;
157 T3 Val3;
158 T4 Val4;
159 T5 Val5;
160 public:
161 format_object5(const char *fmt, const T1 &val1, const T2 &val2,
162 const T3 &val3, const T4 &val4, const T5 &val5)
163 : format_object_base(fmt), Val1(val1), Val2(val2), Val3(val3), Val4(val4),
164 Val5(val5) {
165 }
166
167 virtual int snprint(char *Buffer, unsigned BufferSize) const {
168 return snprintf(Buffer, BufferSize, Fmt, Val1, Val2, Val3, Val4, Val5);
169 }
170 };
171
128172 /// format - This is a helper function that is used to produce formatted output.
129173 /// This is typically used like: OS << format("%0.4f", myfloat) << '\n';
130174 template
148192 return format_object3(Fmt, Val1, Val2, Val3);
149193 }
150194
195 /// format - This is a helper function that is used to produce formatted output.
196 /// This is typically used like: OS << format("%0.4f", myfloat) << '\n';
197 template
198 inline format_object4 format(const char *Fmt, const T1 &Val1,
199 const T2 &Val2, const T3 &Val3,
200 const T4 &Val4) {
201 return format_object4(Fmt, Val1, Val2, Val3, Val4);
202 }
203
204 /// format - This is a helper function that is used to produce formatted output.
205 /// This is typically used like: OS << format("%0.4f", myfloat) << '\n';
206 template
207 inline format_object5 format(const char *Fmt,const T1 &Val1,
208 const T2 &Val2, const T3 &Val3,
209 const T4 &Val4, const T5 &Val5) {
210 return format_object5(Fmt, Val1, Val2, Val3, Val4, Val5);
211 }
212
151213 } // end namespace llvm
152214
153215 #endif
0 ; RUN: llvm-objdump -h %p/TestObjectFiles/trivial-object-test.elf-x86-64 \
1 ; RUN: | FileCheck %s
2
3 ; To verify this, use readelf -S, not objdump -h. Binutils objdump filters the
4 ; results in a way that we don't emulate.
5
6 ; CHECK: Sections:
7 ; CHECK: Idx Name Size Address Type
8 ; CHECK: 0 000000000 00000000000000000
9 ; CHECK: 1 .text 000000026 00000000000000000 TEXT DATA
10 ; CHECK: 2 .rodata.str1.1 00000000d 00000000000000026 DATA
11 ; CHECK: 3 .note.GNU-stack 000000000 00000000000000033
12 ; CHECK: 4 .rela.text 000000048 00000000000000038
13 ; CHECK: 5 .symtab 0000000c0 00000000000000080
14 ; CHECK: 6 .strtab 000000033 00000000000000140
15 ; CHECK: 7 .shstrtab 00000004b 00000000000000173
7272 llvm::ArchName("arch", cl::desc("Target arch to disassemble for, "
7373 "see -version for available targets"));
7474
75 static cl::opt
76 SectionHeaders("section-headers", cl::desc("Display summaries of the headers "
77 "for each section."));
78 static cl::alias
79 SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
80 cl::aliasopt(SectionHeaders));
81 static cl::alias
82 SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
83 cl::aliasopt(SectionHeaders));
84
7585 static StringRef ToolName;
7686
7787 static bool error(error_code ec) {
280290 }
281291 }
282292
293 static void PrintSectionHeaders(const ObjectFile *o) {
294 outs() << "Sections:\n"
295 "Idx Name Size Address Type\n";
296 error_code ec;
297 unsigned i = 0;
298 for (section_iterator si = o->begin_sections(), se = o->end_sections();
299 si != se; si.increment(ec)) {
300 if (error(ec)) return;
301 StringRef Name;
302 if (error(si->getName(Name))) return;
303 uint64_t Address;
304 if (error(si->getAddress(Address))) return;
305 uint64_t Size;
306 if (error(si->getSize(Size))) return;
307 bool Text, Data, BSS;
308 if (error(si->isText(Text))) return;
309 if (error(si->isData(Data))) return;
310 if (error(si->isBSS(BSS))) return;
311 std::string Type = (std::string(Text ? "TEXT " : "") +
312 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
313 outs() << format("%3d %-13s %09x %017x %s\n", i, Name.str().c_str(), Size,
314 Address, Type.c_str());
315 ++i;
316 }
317 }
318
283319 static void DumpObject(const ObjectFile *o) {
284320 if (Disassemble)
285321 DisassembleObject(o);
286322 if (Relocations)
287323 PrintRelocations(o);
324 if (SectionHeaders)
325 PrintSectionHeaders(o);
288326 }
289327
290328 /// @brief Dump each object file in \a a;
355393 if (InputFilenames.size() == 0)
356394 InputFilenames.push_back("a.out");
357395
358 if (!Disassemble && !Relocations) {
396 if (!Disassemble && !Relocations && !SectionHeaders) {
359397 cl::PrintHelpMessage();
360398 return 2;
361399 }