blob: 315118f61091c255a48b6ea501b770dd2a2074d8 [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"
17#include "llvm/Target/TargetData.h"
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000018#include "llvm/Target/TargetMachine.h"
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000019
Bill Wendling40d77642007-01-27 02:54:30 +000020using namespace llvm;
21
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000022//===----------------------------------------------------------------------===//
23// Implementation of the X86ELFWriterInfo class
24//===----------------------------------------------------------------------===//
25
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000026X86ELFWriterInfo::X86ELFWriterInfo(TargetMachine &TM)
27 : TargetELFWriterInfo(TM) {
28 bool is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
29 EMachine = is64Bit ? EM_X86_64 : EM_386;
30 }
31
Bill Wendling0db1f0b2007-01-27 11:40:32 +000032X86ELFWriterInfo::~X86ELFWriterInfo() {}
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000033
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000034unsigned 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
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000062unsigned 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}
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000073
74long 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}