1 //===-- X86ELFWriterInfo.cpp - ELF Writer Info for the X86 backend --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements ELF writer information for the X86 backend.
12 //===----------------------------------------------------------------------===//
14 #include "X86ELFWriterInfo.h"
15 #include "X86Relocations.h"
16 #include "llvm/Function.h"
17 #include "llvm/Support/ELF.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Target/TargetData.h"
20 #include "llvm/Target/TargetMachine.h"
24 //===----------------------------------------------------------------------===//
25 // Implementation of the X86ELFWriterInfo class
26 //===----------------------------------------------------------------------===//
28 X86ELFWriterInfo::X86ELFWriterInfo(bool is64Bit_, bool isLittleEndian_)
29 : TargetELFWriterInfo(is64Bit_, isLittleEndian_) {
30 EMachine = is64Bit ? EM_X86_64 : EM_386;
33 X86ELFWriterInfo::~X86ELFWriterInfo() {}
35 unsigned X86ELFWriterInfo::getRelocationType(unsigned MachineRelTy) const {
37 switch(MachineRelTy) {
38 case X86::reloc_pcrel_word:
39 return ELF::R_X86_64_PC32;
40 case X86::reloc_absolute_word:
41 return ELF::R_X86_64_32;
42 case X86::reloc_absolute_word_sext:
43 return ELF::R_X86_64_32S;
44 case X86::reloc_absolute_dword:
45 return ELF::R_X86_64_64;
46 case X86::reloc_picrel_word:
48 llvm_unreachable("unknown x86_64 machine relocation type");
51 switch(MachineRelTy) {
52 case X86::reloc_pcrel_word:
53 return ELF::R_386_PC32;
54 case X86::reloc_absolute_word:
56 case X86::reloc_absolute_word_sext:
57 case X86::reloc_absolute_dword:
58 case X86::reloc_picrel_word:
60 llvm_unreachable("unknown x86 machine relocation type");
65 long int X86ELFWriterInfo::getDefaultAddendForRelTy(unsigned RelTy,
66 long int Modifier) const {
69 case ELF::R_X86_64_PC32: return Modifier - 4;
70 case ELF::R_X86_64_32:
71 case ELF::R_X86_64_32S:
72 case ELF::R_X86_64_64:
75 llvm_unreachable("unknown x86_64 relocation type");
79 case ELF::R_386_PC32: return Modifier - 4;
80 case ELF::R_386_32: return Modifier;
82 llvm_unreachable("unknown x86 relocation type");
87 unsigned X86ELFWriterInfo::getRelocationTySize(unsigned RelTy) const {
90 case ELF::R_X86_64_PC32:
91 case ELF::R_X86_64_32:
92 case ELF::R_X86_64_32S:
94 case ELF::R_X86_64_64:
97 llvm_unreachable("unknown x86_64 relocation type");
101 case ELF::R_386_PC32:
105 llvm_unreachable("unknown x86 relocation type");
110 bool X86ELFWriterInfo::isPCRelativeRel(unsigned RelTy) const {
113 case ELF::R_X86_64_PC32:
115 case ELF::R_X86_64_32:
116 case ELF::R_X86_64_32S:
117 case ELF::R_X86_64_64:
120 llvm_unreachable("unknown x86_64 relocation type");
124 case ELF::R_386_PC32:
129 llvm_unreachable("unknown x86 relocation type");
134 unsigned X86ELFWriterInfo::getAbsoluteLabelMachineRelTy() const {
136 X86::reloc_absolute_dword : X86::reloc_absolute_word;
139 long int X86ELFWriterInfo::computeRelocation(unsigned SymOffset,
141 unsigned RelTy) const {
143 if (RelTy == ELF::R_X86_64_PC32 || RelTy == ELF::R_386_PC32)
144 return SymOffset - (RelOffset + 4);
146 llvm_unreachable("computeRelocation unknown for this relocation type");