blob: 9e44f97ca800d38802d780cfcb07f01f1dc65956 [file] [log] [blame]
Bill Wendling40d77642007-01-27 02:54:30 +00001//===-- X86ELFWriterInfo.cpp - ELF Writer Info for the X86 backend --------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bill Wendling40d77642007-01-27 02:54:30 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements ELF writer information for the X86 backend.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86ELFWriterInfo.h"
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000015#include "X86Relocations.h"
Bruno Cardoso Lopesd00d4152009-06-11 22:13:00 +000016#include "llvm/Function.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000017#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopesd00d4152009-06-11 22:13:00 +000018#include "llvm/Target/TargetData.h"
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000019#include "llvm/Target/TargetMachine.h"
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000020
Bill Wendling40d77642007-01-27 02:54:30 +000021using namespace llvm;
22
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000023//===----------------------------------------------------------------------===//
24// Implementation of the X86ELFWriterInfo class
25//===----------------------------------------------------------------------===//
26
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000027X86ELFWriterInfo::X86ELFWriterInfo(TargetMachine &TM)
28 : TargetELFWriterInfo(TM) {
29 bool is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
30 EMachine = is64Bit ? EM_X86_64 : EM_386;
31 }
32
Bill Wendling0db1f0b2007-01-27 11:40:32 +000033X86ELFWriterInfo::~X86ELFWriterInfo() {}
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000034
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000035unsigned X86ELFWriterInfo::getRelocationType(unsigned MachineRelTy) const {
36 if (is64Bit) {
37 switch(MachineRelTy) {
38 case X86::reloc_pcrel_word:
39 return R_X86_64_PC32;
40 case X86::reloc_absolute_word:
41 return R_X86_64_32;
42 case X86::reloc_absolute_dword:
43 return R_X86_64_64;
44 case X86::reloc_picrel_word:
45 default:
Bruno Cardoso Lopes171375f2009-07-18 19:30:09 +000046 llvm_unreachable("unknown x86_64 machine relocation type");
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000047 }
48 } else {
49 switch(MachineRelTy) {
50 case X86::reloc_pcrel_word:
51 return R_386_PC32;
52 case X86::reloc_absolute_word:
53 return R_386_32;
54 case X86::reloc_absolute_dword:
55 case X86::reloc_picrel_word:
56 default:
Bruno Cardoso Lopes171375f2009-07-18 19:30:09 +000057 llvm_unreachable("unknown x86 machine relocation type");
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000058 }
59 }
60 return 0;
61}
62
Bruno Cardoso Lopes171375f2009-07-18 19:30:09 +000063long int X86ELFWriterInfo::getDefaultAddendForRelTy(unsigned RelTy) const {
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000064 if (is64Bit) {
65 switch(RelTy) {
66 case R_X86_64_PC32: return -4;
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +000067 case R_X86_64_32: return 0;
Bruno Cardoso Lopes171375f2009-07-18 19:30:09 +000068 default:
69 llvm_unreachable("unknown x86_64 relocation type");
70 }
71 } else {
72 switch(RelTy) {
73 case R_386_PC32: return -4;
74 case R_386_32: return 0;
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000075 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000076 llvm_unreachable("unknown x86 relocation type");
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000077 }
78 }
79 return 0;
80}
Bruno Cardoso Lopes171375f2009-07-18 19:30:09 +000081
82unsigned X86ELFWriterInfo::getRelocationTySize(unsigned RelTy) const {
83 if (is64Bit) {
84 switch(RelTy) {
85 case R_X86_64_PC32:
86 case R_X86_64_32:
Bruno Cardoso Lopese2b0ecd2009-07-18 23:24:01 +000087 case R_X86_64_32S:
Bruno Cardoso Lopes171375f2009-07-18 19:30:09 +000088 return 32;
89 case R_X86_64_64:
90 return 64;
91 default:
92 llvm_unreachable("unknown x86_64 relocation type");
93 }
94 } else {
95 switch(RelTy) {
96 case R_386_PC32:
97 case R_386_32:
98 return 32;
99 default:
100 llvm_unreachable("unknown x86 relocation type");
101 }
102 }
103 return 0;
104}
105
Bruno Cardoso Lopes617dd7b2009-07-18 20:52:11 +0000106unsigned X86ELFWriterInfo::getAbsoluteLabelMachineRelTy() const {
107 return is64Bit ?
108 X86::reloc_absolute_dword : X86::reloc_absolute_word;
Bruno Cardoso Lopes171375f2009-07-18 19:30:09 +0000109}
110