mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-09-25 17:20:48 +00:00
MC/Mach-O: Introduce Object/MachOFormat for describing purely platform / machine
independent information on the Mach object format, and move some stuff from MachObjectWriter.cpp there. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120186 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
112
include/llvm/Object/MachOFormat.h
Normal file
112
include/llvm/Object/MachOFormat.h
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
//===- MachOFormat.h - Mach-O Format Structures And Constants ---*- C++ -*-===//
|
||||||
|
//
|
||||||
|
// The LLVM Compiler Infrastructure
|
||||||
|
//
|
||||||
|
// This file is distributed under the University of Illinois Open Source
|
||||||
|
// License. See LICENSE.TXT for details.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// This file declares various structures and constants which are platform
|
||||||
|
// independent and can be shared by any client which wishes to interact with
|
||||||
|
// Mach object files.
|
||||||
|
//
|
||||||
|
// The definitions here are purposely chosen to match the LLVM style as opposed
|
||||||
|
// to following the platform specific definition of the format.
|
||||||
|
//
|
||||||
|
// On a Mach system, see the <mach-o/...> includes for more information, in
|
||||||
|
// particular <mach-o/loader.h>.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
#ifndef LLVM_OBJECT_MACHOFORMAT_H
|
||||||
|
#define LLVM_OBJECT_MACHOFORMAT_H
|
||||||
|
|
||||||
|
namespace llvm {
|
||||||
|
namespace object {
|
||||||
|
|
||||||
|
/// General Mach platform information.
|
||||||
|
namespace mach {
|
||||||
|
/// @name CPU Type and Subtype Information
|
||||||
|
/// {
|
||||||
|
|
||||||
|
/// \brief Capability bits used in CPU type encoding.
|
||||||
|
enum CPUTypeFlagsMask {
|
||||||
|
CTFM_ArchMask = 0xFF000000,
|
||||||
|
CTFM_ArchABI64 = 0x01000000
|
||||||
|
};
|
||||||
|
|
||||||
|
/// \brief Machine type IDs used in CPU type encoding.
|
||||||
|
enum CPUTypeMachine {
|
||||||
|
CTM_i386 = 7,
|
||||||
|
CTM_x86_64 = CTM_i386 | CTFM_ArchABI64,
|
||||||
|
CTM_ARM = 12,
|
||||||
|
CTM_SPARC = 14,
|
||||||
|
CTM_PowerPC = 18,
|
||||||
|
CTM_PowerPC64 = CTM_PowerPC | CTFM_ArchABI64
|
||||||
|
};
|
||||||
|
|
||||||
|
/// \brief Capability bits used in CPU subtype encoding.
|
||||||
|
enum CPUSubtypeFlagsMask {
|
||||||
|
CSFM_SubtypeMask = 0xFF000000,
|
||||||
|
CSFM_SubtypeLib64 = 0x80000000
|
||||||
|
};
|
||||||
|
|
||||||
|
/// \brief ARM Machine Subtypes.
|
||||||
|
enum CPUSubtypeARM {
|
||||||
|
CSARM_ALL = 0,
|
||||||
|
CSARM_V4T = 5,
|
||||||
|
CSARM_V6 = 6,
|
||||||
|
CSARM_V5TEJ = 7,
|
||||||
|
CSARM_XSCALE = 8,
|
||||||
|
CSARM_V7 = 9
|
||||||
|
};
|
||||||
|
|
||||||
|
/// \brief PowerPC Machine Subtypes.
|
||||||
|
enum CPUSubtypePowerPC {
|
||||||
|
CSPPC_ALL = 0
|
||||||
|
};
|
||||||
|
|
||||||
|
/// \brief SPARC Machine Subtypes.
|
||||||
|
enum CPUSubtypeSPARC {
|
||||||
|
CSSPARC_ALL = 0
|
||||||
|
};
|
||||||
|
|
||||||
|
/// \brief x86 Machine Subtypes.
|
||||||
|
enum CPUSubtypeX86 {
|
||||||
|
CSX86_ALL = 3
|
||||||
|
};
|
||||||
|
|
||||||
|
/// @}
|
||||||
|
|
||||||
|
} // end namespace mach
|
||||||
|
|
||||||
|
/// Format information for Mach object files.
|
||||||
|
namespace macho {
|
||||||
|
/// \brief Constants for header magic field.
|
||||||
|
enum HeaderMagic {
|
||||||
|
HM_Object32 = 0xFEEDFACE, ///< 32-bit mach object file
|
||||||
|
HM_Object64 = 0xFEEDFACF, ///< 64-bit mach object file
|
||||||
|
HM_Universal = 0xCAFEBABE ///< Universal object file
|
||||||
|
};
|
||||||
|
|
||||||
|
/// \brief Constants for structure sizes.
|
||||||
|
enum StructureSizes {
|
||||||
|
Header32Size = 28,
|
||||||
|
Header64Size = 32,
|
||||||
|
SegmentLoadCommand32Size = 56,
|
||||||
|
SegmentLoadCommand64Size = 72,
|
||||||
|
Section32Size = 68,
|
||||||
|
Section64Size = 80,
|
||||||
|
SymtabLoadCommandSize = 24,
|
||||||
|
DysymtabLoadCommandSize = 80,
|
||||||
|
Nlist32Size = 12,
|
||||||
|
Nlist64Size = 16,
|
||||||
|
RelocationInfoSize = 8
|
||||||
|
};
|
||||||
|
} // end namespace macho
|
||||||
|
|
||||||
|
} // end namespace object
|
||||||
|
} // end namespace llvm
|
||||||
|
|
||||||
|
#endif
|
@@ -17,8 +17,8 @@
|
|||||||
#include "llvm/MC/MCSymbol.h"
|
#include "llvm/MC/MCSymbol.h"
|
||||||
#include "llvm/MC/MCMachOSymbolFlags.h"
|
#include "llvm/MC/MCMachOSymbolFlags.h"
|
||||||
#include "llvm/MC/MCValue.h"
|
#include "llvm/MC/MCValue.h"
|
||||||
|
#include "llvm/Object/MachOFormat.h"
|
||||||
#include "llvm/Support/ErrorHandling.h"
|
#include "llvm/Support/ErrorHandling.h"
|
||||||
#include "llvm/Support/MachO.h"
|
|
||||||
#include "llvm/Target/TargetAsmBackend.h"
|
#include "llvm/Target/TargetAsmBackend.h"
|
||||||
|
|
||||||
// FIXME: Gross.
|
// FIXME: Gross.
|
||||||
@@ -26,6 +26,7 @@
|
|||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
|
using namespace llvm::object;
|
||||||
|
|
||||||
// FIXME: this has been copied from (or to) X86AsmBackend.cpp
|
// FIXME: this has been copied from (or to) X86AsmBackend.cpp
|
||||||
static unsigned getFixupKindLog2Size(unsigned Kind) {
|
static unsigned getFixupKindLog2Size(unsigned Kind) {
|
||||||
@@ -160,25 +161,6 @@ namespace {
|
|||||||
|
|
||||||
class MachObjectWriter : public MCObjectWriter {
|
class MachObjectWriter : public MCObjectWriter {
|
||||||
// See <mach-o/loader.h>.
|
// See <mach-o/loader.h>.
|
||||||
enum {
|
|
||||||
Header_Magic32 = 0xFEEDFACE,
|
|
||||||
Header_Magic64 = 0xFEEDFACF
|
|
||||||
};
|
|
||||||
|
|
||||||
enum {
|
|
||||||
Header32Size = 28,
|
|
||||||
Header64Size = 32,
|
|
||||||
SegmentLoadCommand32Size = 56,
|
|
||||||
SegmentLoadCommand64Size = 72,
|
|
||||||
Section32Size = 68,
|
|
||||||
Section64Size = 80,
|
|
||||||
SymtabLoadCommandSize = 24,
|
|
||||||
DysymtabLoadCommandSize = 80,
|
|
||||||
Nlist32Size = 12,
|
|
||||||
Nlist64Size = 16,
|
|
||||||
RelocationInfoSize = 8
|
|
||||||
};
|
|
||||||
|
|
||||||
enum HeaderFileType {
|
enum HeaderFileType {
|
||||||
HFT_Object = 0x1
|
HFT_Object = 0x1
|
||||||
};
|
};
|
||||||
@@ -309,7 +291,7 @@ public:
|
|||||||
uint64_t Start = OS.tell();
|
uint64_t Start = OS.tell();
|
||||||
(void) Start;
|
(void) Start;
|
||||||
|
|
||||||
Write32(Is64Bit ? Header_Magic64 : Header_Magic32);
|
Write32(Is64Bit ? macho::HM_Object64 : macho::HM_Object32);
|
||||||
|
|
||||||
Write32(CPUType);
|
Write32(CPUType);
|
||||||
Write32(CPUSubtype);
|
Write32(CPUSubtype);
|
||||||
@@ -322,7 +304,8 @@ public:
|
|||||||
if (Is64Bit)
|
if (Is64Bit)
|
||||||
Write32(0); // reserved
|
Write32(0); // reserved
|
||||||
|
|
||||||
assert(OS.tell() - Start == Is64Bit ? Header64Size : Header32Size);
|
assert(OS.tell() - Start == Is64Bit ?
|
||||||
|
macho::Header64Size : macho::Header32Size);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// WriteSegmentLoadCommand - Write a segment load command.
|
/// WriteSegmentLoadCommand - Write a segment load command.
|
||||||
@@ -339,11 +322,12 @@ public:
|
|||||||
uint64_t Start = OS.tell();
|
uint64_t Start = OS.tell();
|
||||||
(void) Start;
|
(void) Start;
|
||||||
|
|
||||||
unsigned SegmentLoadCommandSize = Is64Bit ? SegmentLoadCommand64Size :
|
unsigned SegmentLoadCommandSize = Is64Bit ? macho::SegmentLoadCommand64Size:
|
||||||
SegmentLoadCommand32Size;
|
macho::SegmentLoadCommand32Size;
|
||||||
Write32(Is64Bit ? LCT_Segment64 : LCT_Segment);
|
Write32(Is64Bit ? LCT_Segment64 : LCT_Segment);
|
||||||
Write32(SegmentLoadCommandSize +
|
Write32(SegmentLoadCommandSize +
|
||||||
NumSections * (Is64Bit ? Section64Size : Section32Size));
|
NumSections * (Is64Bit ? macho::Section64Size :
|
||||||
|
macho::Section32Size));
|
||||||
|
|
||||||
WriteBytes("", 16);
|
WriteBytes("", 16);
|
||||||
if (Is64Bit) {
|
if (Is64Bit) {
|
||||||
@@ -408,7 +392,8 @@ public:
|
|||||||
if (Is64Bit)
|
if (Is64Bit)
|
||||||
Write32(0); // reserved3
|
Write32(0); // reserved3
|
||||||
|
|
||||||
assert(OS.tell() - Start == Is64Bit ? Section64Size : Section32Size);
|
assert(OS.tell() - Start == Is64Bit ? macho::Section64Size :
|
||||||
|
macho::Section32Size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
|
void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
|
||||||
@@ -420,13 +405,13 @@ public:
|
|||||||
(void) Start;
|
(void) Start;
|
||||||
|
|
||||||
Write32(LCT_Symtab);
|
Write32(LCT_Symtab);
|
||||||
Write32(SymtabLoadCommandSize);
|
Write32(macho::SymtabLoadCommandSize);
|
||||||
Write32(SymbolOffset);
|
Write32(SymbolOffset);
|
||||||
Write32(NumSymbols);
|
Write32(NumSymbols);
|
||||||
Write32(StringTableOffset);
|
Write32(StringTableOffset);
|
||||||
Write32(StringTableSize);
|
Write32(StringTableSize);
|
||||||
|
|
||||||
assert(OS.tell() - Start == SymtabLoadCommandSize);
|
assert(OS.tell() - Start == macho::SymtabLoadCommandSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
|
void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
|
||||||
@@ -443,7 +428,7 @@ public:
|
|||||||
(void) Start;
|
(void) Start;
|
||||||
|
|
||||||
Write32(LCT_Dysymtab);
|
Write32(LCT_Dysymtab);
|
||||||
Write32(DysymtabLoadCommandSize);
|
Write32(macho::DysymtabLoadCommandSize);
|
||||||
Write32(FirstLocalSymbol);
|
Write32(FirstLocalSymbol);
|
||||||
Write32(NumLocalSymbols);
|
Write32(NumLocalSymbols);
|
||||||
Write32(FirstExternalSymbol);
|
Write32(FirstExternalSymbol);
|
||||||
@@ -463,7 +448,7 @@ public:
|
|||||||
Write32(0); // locreloff
|
Write32(0); // locreloff
|
||||||
Write32(0); // nlocrel
|
Write32(0); // nlocrel
|
||||||
|
|
||||||
assert(OS.tell() - Start == DysymtabLoadCommandSize);
|
assert(OS.tell() - Start == macho::DysymtabLoadCommandSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WriteNlist(MachSymbolData &MSD, const MCAsmLayout &Layout) {
|
void WriteNlist(MachSymbolData &MSD, const MCAsmLayout &Layout) {
|
||||||
@@ -1164,21 +1149,22 @@ public:
|
|||||||
// section headers) and the symbol table.
|
// section headers) and the symbol table.
|
||||||
unsigned NumLoadCommands = 1;
|
unsigned NumLoadCommands = 1;
|
||||||
uint64_t LoadCommandsSize = Is64Bit ?
|
uint64_t LoadCommandsSize = Is64Bit ?
|
||||||
SegmentLoadCommand64Size + NumSections * Section64Size :
|
macho::SegmentLoadCommand64Size + NumSections * macho::Section64Size :
|
||||||
SegmentLoadCommand32Size + NumSections * Section32Size;
|
macho::SegmentLoadCommand32Size + NumSections * macho::Section32Size;
|
||||||
|
|
||||||
// Add the symbol table load command sizes, if used.
|
// Add the symbol table load command sizes, if used.
|
||||||
unsigned NumSymbols = LocalSymbolData.size() + ExternalSymbolData.size() +
|
unsigned NumSymbols = LocalSymbolData.size() + ExternalSymbolData.size() +
|
||||||
UndefinedSymbolData.size();
|
UndefinedSymbolData.size();
|
||||||
if (NumSymbols) {
|
if (NumSymbols) {
|
||||||
NumLoadCommands += 2;
|
NumLoadCommands += 2;
|
||||||
LoadCommandsSize += SymtabLoadCommandSize + DysymtabLoadCommandSize;
|
LoadCommandsSize += (macho::SymtabLoadCommandSize +
|
||||||
|
macho::DysymtabLoadCommandSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute the total size of the section data, as well as its file size and
|
// Compute the total size of the section data, as well as its file size and
|
||||||
// vm size.
|
// vm size.
|
||||||
uint64_t SectionDataStart = (Is64Bit ? Header64Size : Header32Size)
|
uint64_t SectionDataStart = (Is64Bit ? macho::Header64Size :
|
||||||
+ LoadCommandsSize;
|
macho::Header32Size) + LoadCommandsSize;
|
||||||
uint64_t SectionDataSize = 0;
|
uint64_t SectionDataSize = 0;
|
||||||
uint64_t SectionDataFileSize = 0;
|
uint64_t SectionDataFileSize = 0;
|
||||||
uint64_t VMSize = 0;
|
uint64_t VMSize = 0;
|
||||||
@@ -1218,7 +1204,7 @@ public:
|
|||||||
unsigned NumRelocs = Relocs.size();
|
unsigned NumRelocs = Relocs.size();
|
||||||
uint64_t SectionStart = SectionDataStart + Layout.getSectionAddress(it);
|
uint64_t SectionStart = SectionDataStart + Layout.getSectionAddress(it);
|
||||||
WriteSection(Asm, Layout, *it, SectionStart, RelocTableEnd, NumRelocs);
|
WriteSection(Asm, Layout, *it, SectionStart, RelocTableEnd, NumRelocs);
|
||||||
RelocTableEnd += NumRelocs * RelocationInfoSize;
|
RelocTableEnd += NumRelocs * macho::RelocationInfoSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the symbol table load command, if used.
|
// Write the symbol table load command, if used.
|
||||||
@@ -1244,8 +1230,8 @@ public:
|
|||||||
|
|
||||||
// The string table is written after symbol table.
|
// The string table is written after symbol table.
|
||||||
uint64_t StringTableOffset =
|
uint64_t StringTableOffset =
|
||||||
SymbolTableOffset + NumSymTabSymbols * (Is64Bit ? Nlist64Size :
|
SymbolTableOffset + NumSymTabSymbols * (Is64Bit ? macho::Nlist64Size :
|
||||||
Nlist32Size);
|
macho::Nlist32Size);
|
||||||
WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols,
|
WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols,
|
||||||
StringTableOffset, StringTable.size());
|
StringTableOffset, StringTable.size());
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user