Fixed some bugs.
[llvm/zpu.git] / lib / Target / X86 / X86ELFWriterInfo.cpp
blob3b721b4fe0eb8ff047f96c3cebf94bc1c726457d
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.
12 //===----------------------------------------------------------------------===//
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"
21 using namespace llvm;
23 //===----------------------------------------------------------------------===//
24 // Implementation of the X86ELFWriterInfo class
25 //===----------------------------------------------------------------------===//
27 X86ELFWriterInfo::X86ELFWriterInfo(bool is64Bit_, bool isLittleEndian_)
28 : TargetELFWriterInfo(is64Bit_, isLittleEndian_) {
29 EMachine = is64Bit ? EM_X86_64 : EM_386;
32 X86ELFWriterInfo::~X86ELFWriterInfo() {}
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");
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");
62 return 0;
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");
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");
85 return 0;
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");
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");
109 return 0;
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");
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");
134 return 0;
137 unsigned X86ELFWriterInfo::getAbsoluteLabelMachineRelTy() const {
138 return is64Bit ?
139 X86::reloc_absolute_dword : X86::reloc_absolute_word;
142 long int X86ELFWriterInfo::computeRelocation(unsigned SymOffset,
143 unsigned RelOffset,
144 unsigned RelTy) const {
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");
151 return 0;