[llvm-commits] [llvm] r120216 - in /llvm/trunk: include/llvm/Object/MachOFormat.h include/llvm/Object/MachOObject.h lib/MC/MachObjectWriter.cpp lib/Object/MachOObject.cpp tools/macho-dump/macho-dump.cpp
Daniel Dunbar
daniel at zuster.org
Sat Nov 27 05:39:48 PST 2010
Author: ddunbar
Date: Sat Nov 27 07:39:48 2010
New Revision: 120216
URL: http://llvm.org/viewvc/llvm-project?rev=120216&view=rev
Log:
macho-dump: Add support for dumping relocation entries.
Modified:
llvm/trunk/include/llvm/Object/MachOFormat.h
llvm/trunk/include/llvm/Object/MachOObject.h
llvm/trunk/lib/MC/MachObjectWriter.cpp
llvm/trunk/lib/Object/MachOObject.cpp
llvm/trunk/tools/macho-dump/macho-dump.cpp
Modified: llvm/trunk/include/llvm/Object/MachOFormat.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/MachOFormat.h?rev=120216&r1=120215&r2=120216&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/MachOFormat.h (original)
+++ llvm/trunk/include/llvm/Object/MachOFormat.h Sat Nov 27 07:39:48 2010
@@ -259,6 +259,15 @@
};
/// @}
+ /// @name Relocation Data
+ /// @{
+
+ struct RelocationEntry {
+ uint32_t Word0;
+ uint32_t Word1;
+ };
+
+ /// @}
// See <mach-o/nlist.h>.
enum SymbolTypeType {
Modified: llvm/trunk/include/llvm/Object/MachOObject.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/MachOObject.h?rev=120216&r1=120215&r2=120216&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/MachOObject.h (original)
+++ llvm/trunk/include/llvm/Object/MachOObject.h Sat Nov 27 07:39:48 2010
@@ -137,6 +137,9 @@
const LoadCommandInfo &LCI,
unsigned Index,
InMemoryStruct<macho::Section64> &Res) const;
+ void ReadRelocationEntry(
+ uint64_t RelocationTableOffset, unsigned Index,
+ InMemoryStruct<macho::RelocationEntry> &Res) const;
/// @}
};
Modified: llvm/trunk/lib/MC/MachObjectWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MachObjectWriter.cpp?rev=120216&r1=120215&r2=120216&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MachObjectWriter.cpp (original)
+++ llvm/trunk/lib/MC/MachObjectWriter.cpp Sat Nov 27 07:39:48 2010
@@ -177,13 +177,8 @@
/// @name Relocation Data
/// @{
- struct MachRelocationEntry {
- uint32_t Word0;
- uint32_t Word1;
- };
-
llvm::DenseMap<const MCSectionData*,
- std::vector<MachRelocationEntry> > Relocations;
+ std::vector<macho::RelocationEntry> > Relocations;
llvm::DenseMap<const MCSectionData*, unsigned> IndirectSymBase;
/// @}
@@ -551,7 +546,7 @@
}
Type = macho::RIT_X86_64_Unsigned;
- MachRelocationEntry MRE;
+ macho::RelocationEntry MRE;
MRE.Word0 = FixupOffset;
MRE.Word1 = ((Index << 0) |
(IsPCRel << 24) |
@@ -676,7 +671,7 @@
FixedValue = Value;
// struct relocation_info (8 bytes)
- MachRelocationEntry MRE;
+ macho::RelocationEntry MRE;
MRE.Word0 = FixupOffset;
MRE.Word1 = ((Index << 0) |
(IsPCRel << 24) |
@@ -726,7 +721,7 @@
// Relocations are written out in reverse order, so the PAIR comes first.
if (Type == macho::RIT_Difference || Type == macho::RIT_LocalDifference) {
- MachRelocationEntry MRE;
+ macho::RelocationEntry MRE;
MRE.Word0 = ((0 << 0) |
(macho::RIT_Pair << 24) |
(Log2Size << 28) |
@@ -736,7 +731,7 @@
Relocations[Fragment->getParent()].push_back(MRE);
}
- MachRelocationEntry MRE;
+ macho::RelocationEntry MRE;
MRE.Word0 = ((FixupOffset << 0) |
(Type << 24) |
(Log2Size << 28) |
@@ -781,7 +776,7 @@
}
// struct relocation_info (8 bytes)
- MachRelocationEntry MRE;
+ macho::RelocationEntry MRE;
MRE.Word0 = Value;
MRE.Word1 = ((Index << 0) |
(IsPCRel << 24) |
@@ -861,7 +856,7 @@
}
// struct relocation_info (8 bytes)
- MachRelocationEntry MRE;
+ macho::RelocationEntry MRE;
MRE.Word0 = FixupOffset;
MRE.Word1 = ((Index << 0) |
(IsPCRel << 24) |
@@ -1138,7 +1133,7 @@
uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
for (MCAssembler::const_iterator it = Asm.begin(),
ie = Asm.end(); it != ie; ++it) {
- std::vector<MachRelocationEntry> &Relocs = Relocations[it];
+ std::vector<macho::RelocationEntry> &Relocs = Relocations[it];
unsigned NumRelocs = Relocs.size();
uint64_t SectionStart = SectionDataStart + Layout.getSectionAddress(it);
WriteSection(Asm, Layout, *it, SectionStart, RelocTableEnd, NumRelocs);
@@ -1192,7 +1187,7 @@
ie = Asm.end(); it != ie; ++it) {
// Write the section relocation entries, in reverse order to match 'as'
// (approximately, the exact algorithm is more complicated than this).
- std::vector<MachRelocationEntry> &Relocs = Relocations[it];
+ std::vector<macho::RelocationEntry> &Relocs = Relocations[it];
for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
Write32(Relocs[e - i - 1].Word0);
Write32(Relocs[e - i - 1].Word1);
Modified: llvm/trunk/lib/Object/MachOObject.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/MachOObject.cpp?rev=120216&r1=120215&r2=120216&view=diff
==============================================================================
--- llvm/trunk/lib/Object/MachOObject.cpp (original)
+++ llvm/trunk/lib/Object/MachOObject.cpp Sat Nov 27 07:39:48 2010
@@ -289,3 +289,16 @@
Index * sizeof(macho::Section64));
ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
}
+
+template<>
+void SwapStruct(macho::RelocationEntry &Value) {
+ SwapValue(Value.Word0);
+ SwapValue(Value.Word1);
+}
+void MachOObject::ReadRelocationEntry(uint64_t RelocationTableOffset,
+ unsigned Index,
+ InMemoryStruct<macho::RelocationEntry> &Res) const {
+ uint64_t Offset = (RelocationTableOffset +
+ Index * sizeof(macho::RelocationEntry));
+ ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
+}
Modified: llvm/trunk/tools/macho-dump/macho-dump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/macho-dump/macho-dump.cpp?rev=120216&r1=120215&r2=120216&view=diff
==============================================================================
--- llvm/trunk/tools/macho-dump/macho-dump.cpp (original)
+++ llvm/trunk/tools/macho-dump/macho-dump.cpp Sat Nov 27 07:39:48 2010
@@ -83,13 +83,13 @@
outs() << " ('flags', " << Flags << ")\n";
}
-static void DumpSectionData(unsigned Index, StringRef Name,
- StringRef SegmentName, uint64_t Address,
- uint64_t Size, uint32_t Offset,
- uint32_t Align, uint32_t RelocationTableOffset,
- uint32_t NumRelocationTableEntries,
- uint32_t Flags, uint32_t Reserved1,
- uint32_t Reserved2, uint64_t Reserved3 = ~0ULL) {
+static int DumpSectionData(MachOObject &Obj, unsigned Index, StringRef Name,
+ StringRef SegmentName, uint64_t Address,
+ uint64_t Size, uint32_t Offset,
+ uint32_t Align, uint32_t RelocationTableOffset,
+ uint32_t NumRelocationTableEntries,
+ uint32_t Flags, uint32_t Reserved1,
+ uint32_t Reserved2, uint64_t Reserved3 = ~0ULL) {
outs() << " # Section " << Index << "\n";
outs() << " (('section_name', '";
outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
@@ -106,6 +106,26 @@
outs() << " ('reserved2', " << Reserved2 << ")\n";
if (Reserved3 != ~0ULL)
outs() << " ('reserved3', " << Reserved3 << ")\n";
+ outs() << " ),\n";
+
+ // Dump the relocation entries.
+ int Res = 0;
+ outs() << " ('_relocations', [\n";
+ for (unsigned i = 0; i != NumRelocationTableEntries; ++i) {
+ InMemoryStruct<macho::RelocationEntry> RE;
+ Obj.ReadRelocationEntry(RelocationTableOffset, i, RE);
+ if (!RE) {
+ Res = Error("unable to read relocation table entry '" + Twine(i) + "'");
+ break;
+ }
+
+ outs() << " # Relocation " << i << "\n";
+ outs() << " (('word-0', " << format("%#x", RE->Word0) << "),\n";
+ outs() << " ('word-1', " << format("%#x", RE->Word1) << ")),\n";
+ }
+ outs() << " ])\n";
+
+ return Res;
}
static int DumpSegmentCommand(MachOObject &Obj,
@@ -131,12 +151,13 @@
break;
}
- DumpSectionData(i, StringRef(Sect->Name, 16),
- StringRef(Sect->SegmentName, 16), Sect->Address, Sect->Size,
- Sect->Offset, Sect->Align, Sect->RelocationTableOffset,
- Sect->NumRelocationTableEntries, Sect->Flags,
- Sect->Reserved1, Sect->Reserved2);
- outs() << " ),\n";
+ if ((Res = DumpSectionData(Obj, i, StringRef(Sect->Name, 16),
+ StringRef(Sect->SegmentName, 16), Sect->Address,
+ Sect->Size, Sect->Offset, Sect->Align,
+ Sect->RelocationTableOffset,
+ Sect->NumRelocationTableEntries, Sect->Flags,
+ Sect->Reserved1, Sect->Reserved2)))
+ break;
}
outs() << " ])\n";
@@ -166,12 +187,14 @@
break;
}
- DumpSectionData(i, StringRef(Sect->Name, 16),
- StringRef(Sect->SegmentName, 16), Sect->Address, Sect->Size,
- Sect->Offset, Sect->Align, Sect->RelocationTableOffset,
- Sect->NumRelocationTableEntries, Sect->Flags,
- Sect->Reserved1, Sect->Reserved2, Sect->Reserved3);
- outs() << " ),\n";
+ if ((Res = DumpSectionData(Obj, i, StringRef(Sect->Name, 16),
+ StringRef(Sect->SegmentName, 16), Sect->Address,
+ Sect->Size, Sect->Offset, Sect->Align,
+ Sect->RelocationTableOffset,
+ Sect->NumRelocationTableEntries, Sect->Flags,
+ Sect->Reserved1, Sect->Reserved2,
+ Sect->Reserved3)))
+ break;
}
outs() << " ])\n";
More information about the llvm-commits
mailing list