Revert "CMake: Get rid of LLVMLibDeps.cmake and export the libraries normally."
[oota-llvm.git] / lib / Target / X86 / X86ELFWriterInfo.cpp
1 //===-- X86ELFWriterInfo.cpp - ELF Writer Info for the X86 backend --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements ELF writer information for the X86 backend.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "X86ELFWriterInfo.h"
15 #include "X86Relocations.h"
16 #include "llvm/Function.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Target/TargetData.h"
19 #include "llvm/Target/TargetMachine.h"
20
21 using namespace llvm;
22
23 //===----------------------------------------------------------------------===//
24 //  Implementation of the X86ELFWriterInfo class
25 //===----------------------------------------------------------------------===//
26
27 X86ELFWriterInfo::X86ELFWriterInfo(TargetMachine &TM)
28   : TargetELFWriterInfo(TM) {
29     EMachine = is64Bit ? EM_X86_64 : EM_386;
30   }
31
32 X86ELFWriterInfo::~X86ELFWriterInfo() {}
33
34 unsigned X86ELFWriterInfo::getRelocationType(unsigned MachineRelTy) const {
35   if (is64Bit) {
36     switch(MachineRelTy) {
37     case X86::reloc_pcrel_word:
38       return R_X86_64_PC32;
39     case X86::reloc_absolute_word:
40       return R_X86_64_32;
41     case X86::reloc_absolute_word_sext:
42       return R_X86_64_32S;
43     case X86::reloc_absolute_dword:
44       return R_X86_64_64;
45     case X86::reloc_picrel_word:
46     default:
47       llvm_unreachable("unknown x86_64 machine relocation type");
48     }
49   } else {
50     switch(MachineRelTy) {
51     case X86::reloc_pcrel_word:
52       return R_386_PC32;
53     case X86::reloc_absolute_word:
54       return R_386_32;
55     case X86::reloc_absolute_word_sext:
56     case X86::reloc_absolute_dword:
57     case X86::reloc_picrel_word:
58     default:
59       llvm_unreachable("unknown x86 machine relocation type");
60     }
61   }
62   return 0;
63 }
64
65 long int X86ELFWriterInfo::getDefaultAddendForRelTy(unsigned RelTy,
66                                                     long int Modifier) const {
67   if (is64Bit) {
68     switch(RelTy) {
69     case R_X86_64_PC32: return Modifier - 4;
70     case R_X86_64_32:
71     case R_X86_64_32S:
72     case R_X86_64_64:
73       return Modifier;
74     default:
75       llvm_unreachable("unknown x86_64 relocation type");
76     }
77   } else {
78     switch(RelTy) {
79       case R_386_PC32: return Modifier - 4;
80       case R_386_32: return Modifier;
81     default:
82       llvm_unreachable("unknown x86 relocation type");
83     }
84   }
85   return 0;
86 }
87
88 unsigned X86ELFWriterInfo::getRelocationTySize(unsigned RelTy) const {
89   if (is64Bit) {
90     switch(RelTy) {
91       case R_X86_64_PC32:
92       case R_X86_64_32:
93       case R_X86_64_32S:
94         return 32;
95       case R_X86_64_64:
96         return 64;
97     default:
98       llvm_unreachable("unknown x86_64 relocation type");
99     }
100   } else {
101     switch(RelTy) {
102       case R_386_PC32:
103       case R_386_32:
104         return 32;
105     default:
106       llvm_unreachable("unknown x86 relocation type");
107     }
108   }
109   return 0;
110 }
111
112 bool X86ELFWriterInfo::isPCRelativeRel(unsigned RelTy) const {
113   if (is64Bit) {
114     switch(RelTy) {
115       case R_X86_64_PC32:
116         return true;
117       case R_X86_64_32:
118       case R_X86_64_32S:
119       case R_X86_64_64:
120         return false;
121     default:
122       llvm_unreachable("unknown x86_64 relocation type");
123     }
124   } else {
125     switch(RelTy) {
126       case R_386_PC32:
127         return true;
128       case R_386_32:
129         return false;
130     default:
131       llvm_unreachable("unknown x86 relocation type");
132     }
133   }
134   return 0;
135 }
136
137 unsigned X86ELFWriterInfo::getAbsoluteLabelMachineRelTy() const {
138   return is64Bit ?
139     X86::reloc_absolute_dword : X86::reloc_absolute_word;
140 }
141
142 long int X86ELFWriterInfo::computeRelocation(unsigned SymOffset,
143                                              unsigned RelOffset,
144                                              unsigned RelTy) const {
145
146   if (RelTy == R_X86_64_PC32 || RelTy == R_386_PC32)
147     return SymOffset - (RelOffset + 4);
148   else
149     assert("computeRelocation unknown for this relocation type");
150
151   return 0;
152 }