2007-01-27 03:54:30 +01:00
|
|
|
//===-- X86ELFWriterInfo.cpp - ELF Writer Info for the X86 backend --------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-01-27 03:54:30 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements ELF writer information for the X86 backend.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "X86ELFWriterInfo.h"
|
2009-06-22 21:16:16 +02:00
|
|
|
#include "X86Relocations.h"
|
2009-06-12 00:13:00 +02:00
|
|
|
#include "llvm/Function.h"
|
2010-11-22 22:49:05 +01:00
|
|
|
#include "llvm/Support/ELF.h"
|
2009-07-11 22:10:48 +02:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-06-12 00:13:00 +02:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2009-06-11 21:16:03 +02:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2009-06-22 21:16:16 +02:00
|
|
|
|
2007-01-27 03:54:30 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-06-22 21:16:16 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Implementation of the X86ELFWriterInfo class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-10-03 20:59:45 +02:00
|
|
|
X86ELFWriterInfo::X86ELFWriterInfo(bool is64Bit_, bool isLittleEndian_)
|
|
|
|
: TargetELFWriterInfo(is64Bit_, isLittleEndian_) {
|
2009-06-11 21:16:03 +02:00
|
|
|
EMachine = is64Bit ? EM_X86_64 : EM_386;
|
|
|
|
}
|
|
|
|
|
2007-01-27 12:40:32 +01:00
|
|
|
X86ELFWriterInfo::~X86ELFWriterInfo() {}
|
2009-06-11 21:16:03 +02:00
|
|
|
|
2009-06-22 21:16:16 +02:00
|
|
|
unsigned X86ELFWriterInfo::getRelocationType(unsigned MachineRelTy) const {
|
|
|
|
if (is64Bit) {
|
|
|
|
switch(MachineRelTy) {
|
|
|
|
case X86::reloc_pcrel_word:
|
2010-11-22 22:49:05 +01:00
|
|
|
return ELF::R_X86_64_PC32;
|
2009-06-22 21:16:16 +02:00
|
|
|
case X86::reloc_absolute_word:
|
2010-11-22 22:49:05 +01:00
|
|
|
return ELF::R_X86_64_32;
|
2009-08-05 08:57:03 +02:00
|
|
|
case X86::reloc_absolute_word_sext:
|
2010-11-22 22:49:05 +01:00
|
|
|
return ELF::R_X86_64_32S;
|
2009-06-22 21:16:16 +02:00
|
|
|
case X86::reloc_absolute_dword:
|
2010-11-22 22:49:05 +01:00
|
|
|
return ELF::R_X86_64_64;
|
2009-06-22 21:16:16 +02:00
|
|
|
case X86::reloc_picrel_word:
|
|
|
|
default:
|
2009-07-18 21:30:09 +02:00
|
|
|
llvm_unreachable("unknown x86_64 machine relocation type");
|
2009-06-22 21:16:16 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch(MachineRelTy) {
|
|
|
|
case X86::reloc_pcrel_word:
|
2010-11-22 22:49:05 +01:00
|
|
|
return ELF::R_386_PC32;
|
2009-06-22 21:16:16 +02:00
|
|
|
case X86::reloc_absolute_word:
|
2010-11-22 22:49:05 +01:00
|
|
|
return ELF::R_386_32;
|
2009-08-05 08:57:03 +02:00
|
|
|
case X86::reloc_absolute_word_sext:
|
2009-06-22 21:16:16 +02:00
|
|
|
case X86::reloc_absolute_dword:
|
|
|
|
case X86::reloc_picrel_word:
|
|
|
|
default:
|
2009-07-18 21:30:09 +02:00
|
|
|
llvm_unreachable("unknown x86 machine relocation type");
|
2009-06-22 21:16:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-05 08:57:03 +02:00
|
|
|
long int X86ELFWriterInfo::getDefaultAddendForRelTy(unsigned RelTy,
|
|
|
|
long int Modifier) const {
|
2009-06-22 21:16:16 +02:00
|
|
|
if (is64Bit) {
|
|
|
|
switch(RelTy) {
|
2010-11-22 22:49:05 +01:00
|
|
|
case ELF::R_X86_64_PC32: return Modifier - 4;
|
|
|
|
case ELF::R_X86_64_32:
|
|
|
|
case ELF::R_X86_64_32S:
|
|
|
|
case ELF::R_X86_64_64:
|
2009-08-05 08:57:03 +02:00
|
|
|
return Modifier;
|
2009-07-18 21:30:09 +02:00
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown x86_64 relocation type");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch(RelTy) {
|
2010-11-22 22:49:05 +01:00
|
|
|
case ELF::R_386_PC32: return Modifier - 4;
|
|
|
|
case ELF::R_386_32: return Modifier;
|
2009-07-18 21:30:09 +02:00
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown x86 relocation type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned X86ELFWriterInfo::getRelocationTySize(unsigned RelTy) const {
|
|
|
|
if (is64Bit) {
|
|
|
|
switch(RelTy) {
|
2010-11-22 22:49:05 +01:00
|
|
|
case ELF::R_X86_64_PC32:
|
|
|
|
case ELF::R_X86_64_32:
|
|
|
|
case ELF::R_X86_64_32S:
|
2009-07-18 21:30:09 +02:00
|
|
|
return 32;
|
2010-11-22 22:49:05 +01:00
|
|
|
case ELF::R_X86_64_64:
|
2009-07-18 21:30:09 +02:00
|
|
|
return 64;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown x86_64 relocation type");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch(RelTy) {
|
2010-11-22 22:49:05 +01:00
|
|
|
case ELF::R_386_PC32:
|
|
|
|
case ELF::R_386_32:
|
2009-07-18 21:30:09 +02:00
|
|
|
return 32;
|
2009-06-22 21:16:16 +02:00
|
|
|
default:
|
2009-07-14 18:55:14 +02:00
|
|
|
llvm_unreachable("unknown x86 relocation type");
|
2009-06-22 21:16:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2009-07-18 21:30:09 +02:00
|
|
|
|
2009-07-20 10:52:02 +02:00
|
|
|
bool X86ELFWriterInfo::isPCRelativeRel(unsigned RelTy) const {
|
|
|
|
if (is64Bit) {
|
|
|
|
switch(RelTy) {
|
2010-11-22 22:49:05 +01:00
|
|
|
case ELF::R_X86_64_PC32:
|
2009-07-20 10:52:02 +02:00
|
|
|
return true;
|
2010-11-22 22:49:05 +01:00
|
|
|
case ELF::R_X86_64_32:
|
|
|
|
case ELF::R_X86_64_32S:
|
|
|
|
case ELF::R_X86_64_64:
|
2009-07-20 10:52:02 +02:00
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown x86_64 relocation type");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch(RelTy) {
|
2010-11-22 22:49:05 +01:00
|
|
|
case ELF::R_386_PC32:
|
2009-07-20 10:52:02 +02:00
|
|
|
return true;
|
2010-11-22 22:49:05 +01:00
|
|
|
case ELF::R_386_32:
|
2009-07-20 10:52:02 +02:00
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown x86 relocation type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-18 22:52:11 +02:00
|
|
|
unsigned X86ELFWriterInfo::getAbsoluteLabelMachineRelTy() const {
|
|
|
|
return is64Bit ?
|
|
|
|
X86::reloc_absolute_dword : X86::reloc_absolute_word;
|
2009-07-18 21:30:09 +02:00
|
|
|
}
|
|
|
|
|
2009-07-20 10:52:02 +02:00
|
|
|
long int X86ELFWriterInfo::computeRelocation(unsigned SymOffset,
|
|
|
|
unsigned RelOffset,
|
|
|
|
unsigned RelTy) const {
|
|
|
|
|
2010-11-22 22:49:05 +01:00
|
|
|
if (RelTy == ELF::R_X86_64_PC32 || RelTy == ELF::R_386_PC32)
|
2009-07-20 10:52:02 +02:00
|
|
|
return SymOffset - (RelOffset + 4);
|
|
|
|
else
|
2011-09-10 03:42:07 +02:00
|
|
|
assert(0 && "computeRelocation unknown for this relocation type");
|
2009-07-20 10:52:02 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|