LLVM API Documentation

X86ELFWriterInfo.cpp

Go to the documentation of this file.
00001 //===-- X86ELFWriterInfo.cpp - ELF Writer Info for the X86 backend --------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements ELF writer information for the X86 backend.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "X86ELFWriterInfo.h"
00015 #include "X86Relocations.h"
00016 #include "llvm/Function.h"
00017 #include "llvm/Support/ErrorHandling.h"
00018 #include "llvm/Target/TargetData.h"
00019 #include "llvm/Target/TargetMachine.h"
00020 
00021 using namespace llvm;
00022 
00023 //===----------------------------------------------------------------------===//
00024 //  Implementation of the X86ELFWriterInfo class
00025 //===----------------------------------------------------------------------===//
00026 
00027 X86ELFWriterInfo::X86ELFWriterInfo(TargetMachine &TM)
00028   : TargetELFWriterInfo(TM) {
00029     bool is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
00030     EMachine = is64Bit ? EM_X86_64 : EM_386;
00031   }
00032 
00033 X86ELFWriterInfo::~X86ELFWriterInfo() {}
00034 
00035 unsigned X86ELFWriterInfo::getRelocationType(unsigned MachineRelTy) const {
00036   if (is64Bit) {
00037     switch(MachineRelTy) {
00038     case X86::reloc_pcrel_word:
00039       return R_X86_64_PC32;
00040     case X86::reloc_absolute_word:
00041       return R_X86_64_32;
00042     case X86::reloc_absolute_word_sext:
00043       return R_X86_64_32S;
00044     case X86::reloc_absolute_dword:
00045       return R_X86_64_64;
00046     case X86::reloc_picrel_word:
00047     default:
00048       llvm_unreachable("unknown x86_64 machine relocation type");
00049     }
00050   } else {
00051     switch(MachineRelTy) {
00052     case X86::reloc_pcrel_word:
00053       return R_386_PC32;
00054     case X86::reloc_absolute_word:
00055       return R_386_32;
00056     case X86::reloc_absolute_word_sext:
00057     case X86::reloc_absolute_dword:
00058     case X86::reloc_picrel_word:
00059     default:
00060       llvm_unreachable("unknown x86 machine relocation type");
00061     }
00062   }
00063   return 0;
00064 }
00065 
00066 long int X86ELFWriterInfo::getDefaultAddendForRelTy(unsigned RelTy,
00067                                                     long int Modifier) const {
00068   if (is64Bit) {
00069     switch(RelTy) {
00070     case R_X86_64_PC32: return Modifier - 4;
00071     case R_X86_64_32:
00072     case R_X86_64_32S:
00073     case R_X86_64_64:
00074       return Modifier;
00075     default:
00076       llvm_unreachable("unknown x86_64 relocation type");
00077     }
00078   } else {
00079     switch(RelTy) {
00080       case R_386_PC32: return Modifier - 4;
00081       case R_386_32: return Modifier;
00082     default:
00083       llvm_unreachable("unknown x86 relocation type");
00084     }
00085   }
00086   return 0;
00087 }
00088 
00089 unsigned X86ELFWriterInfo::getRelocationTySize(unsigned RelTy) const {
00090   if (is64Bit) {
00091     switch(RelTy) {
00092       case R_X86_64_PC32:
00093       case R_X86_64_32:
00094       case R_X86_64_32S:
00095         return 32;
00096       case R_X86_64_64:
00097         return 64;
00098     default:
00099       llvm_unreachable("unknown x86_64 relocation type");
00100     }
00101   } else {
00102     switch(RelTy) {
00103       case R_386_PC32:
00104       case R_386_32:
00105         return 32;
00106     default:
00107       llvm_unreachable("unknown x86 relocation type");
00108     }
00109   }
00110   return 0;
00111 }
00112 
00113 bool X86ELFWriterInfo::isPCRelativeRel(unsigned RelTy) const {
00114   if (is64Bit) {
00115     switch(RelTy) {
00116       case R_X86_64_PC32:
00117         return true;
00118       case R_X86_64_32:
00119       case R_X86_64_32S:
00120       case R_X86_64_64:
00121         return false;
00122     default:
00123       llvm_unreachable("unknown x86_64 relocation type");
00124     }
00125   } else {
00126     switch(RelTy) {
00127       case R_386_PC32:
00128         return true;
00129       case R_386_32:
00130         return false;
00131     default:
00132       llvm_unreachable("unknown x86 relocation type");
00133     }
00134   }
00135   return 0;
00136 }
00137 
00138 unsigned X86ELFWriterInfo::getAbsoluteLabelMachineRelTy() const {
00139   return is64Bit ?
00140     X86::reloc_absolute_dword : X86::reloc_absolute_word;
00141 }
00142 
00143 long int X86ELFWriterInfo::computeRelocation(unsigned SymOffset,
00144                                              unsigned RelOffset,
00145                                              unsigned RelTy) const {
00146 
00147   if (RelTy == R_X86_64_PC32 || RelTy == R_386_PC32)
00148     return SymOffset - (RelOffset + 4);
00149   else
00150     assert("computeRelocation unknown for this relocation type");
00151 
00152   return 0;
00153 }