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