315118f61091c255a48b6ea501b770dd2a2074d8
[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/Target/TargetData.h"
18 #include "llvm/Target/TargetMachine.h"
19
20 using namespace llvm;
21
22 //===----------------------------------------------------------------------===//
23 //  Implementation of the X86ELFWriterInfo class
24 //===----------------------------------------------------------------------===//
25
26 X86ELFWriterInfo::X86ELFWriterInfo(TargetMachine &TM)
27   : TargetELFWriterInfo(TM) {
28     bool is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
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_dword:
42       return R_X86_64_64;
43     case X86::reloc_picrel_word:
44     default:
45       assert(0 && "unknown relocation type");
46     }
47   } else {
48     switch(MachineRelTy) {
49     case X86::reloc_pcrel_word:
50       return R_386_PC32;
51     case X86::reloc_absolute_word:
52       return R_386_32;
53     case X86::reloc_absolute_dword:
54     case X86::reloc_picrel_word:
55     default:
56       assert(0 && "unknown relocation type");
57     }
58   }
59   return 0;
60 }
61
62 unsigned X86ELFWriterInfo::getFunctionAlignment(const Function *F) const {
63   unsigned FnAlign = 4;
64
65   if (F->hasFnAttr(Attribute::OptimizeForSize))
66     FnAlign = 1;
67
68   if (F->getAlignment())
69     FnAlign = Log2_32(F->getAlignment());
70
71   return (1 << FnAlign);
72 }
73
74 long int X86ELFWriterInfo::getAddendForRelTy(unsigned RelTy) const {
75   if (is64Bit) {
76     switch(RelTy) {
77     case R_X86_64_PC32: return -4;
78       break;
79     default:
80       assert(0 && "unknown x86 relocation type");
81     }
82   }
83   return 0;
84 }