MipsELFObjectWriter.cpp revision 239462
1//===-- MipsELFObjectWriter.cpp - Mips ELF Writer -------------------------===//
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#include "MCTargetDesc/MipsBaseInfo.h"
11#include "MCTargetDesc/MipsFixupKinds.h"
12#include "MCTargetDesc/MipsMCTargetDesc.h"
13#include "llvm/MC/MCAssembler.h"
14#include "llvm/MC/MCELFObjectWriter.h"
15#include "llvm/MC/MCExpr.h"
16#include "llvm/MC/MCSection.h"
17#include "llvm/MC/MCValue.h"
18#include "llvm/Support/ErrorHandling.h"
19#include <list>
20
21using namespace llvm;
22
23namespace {
24  struct RelEntry {
25    RelEntry(const ELFRelocationEntry &R, const MCSymbol *S, int64_t O) :
26      Reloc(R), Sym(S), Offset(O) {}
27    ELFRelocationEntry Reloc;
28    const MCSymbol *Sym;
29    int64_t Offset;
30  };
31
32  typedef std::list<RelEntry> RelLs;
33  typedef RelLs::iterator RelLsIter;
34
35  class MipsELFObjectWriter : public MCELFObjectTargetWriter {
36  public:
37    MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI, bool _isN64);
38
39    virtual ~MipsELFObjectWriter();
40
41    virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
42                                  bool IsPCRel, bool IsRelocWithSymbol,
43                                  int64_t Addend) const;
44    virtual unsigned getEFlags() const;
45    virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
46                                           const MCValue &Target,
47                                           const MCFragment &F,
48                                           const MCFixup &Fixup,
49                                           bool IsPCRel) const;
50    virtual void sortRelocs(const MCAssembler &Asm,
51                            std::vector<ELFRelocationEntry> &Relocs);
52  };
53}
54
55MipsELFObjectWriter::MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI,
56                                         bool _isN64)
57  : MCELFObjectTargetWriter(_is64Bit, OSABI, ELF::EM_MIPS,
58                            /*HasRelocationAddend*/ false,
59                            /*IsN64*/ _isN64) {}
60
61MipsELFObjectWriter::~MipsELFObjectWriter() {}
62
63// FIXME: get the real EABI Version from the Subtarget class.
64unsigned MipsELFObjectWriter::getEFlags() const {
65
66  // FIXME: We can't tell if we are PIC (dynamic) or CPIC (static)
67  unsigned Flag = ELF::EF_MIPS_NOREORDER;
68
69  if (is64Bit())
70    Flag |= ELF::EF_MIPS_ARCH_64R2;
71  else
72    Flag |= ELF::EF_MIPS_ARCH_32R2;
73  return Flag;
74}
75
76const MCSymbol *MipsELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
77                                                    const MCValue &Target,
78                                                    const MCFragment &F,
79                                                    const MCFixup &Fixup,
80                                                    bool IsPCRel) const {
81  assert(Target.getSymA() && "SymA cannot be 0.");
82  const MCSymbol &Sym = Target.getSymA()->getSymbol().AliasedSymbol();
83
84  if (Sym.getSection().getKind().isMergeableCString() ||
85      Sym.getSection().getKind().isMergeableConst())
86    return &Sym;
87
88  return NULL;
89}
90
91unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target,
92                                           const MCFixup &Fixup,
93                                           bool IsPCRel,
94                                           bool IsRelocWithSymbol,
95                                           int64_t Addend) const {
96  // determine the type of the relocation
97  unsigned Type = (unsigned)ELF::R_MIPS_NONE;
98  unsigned Kind = (unsigned)Fixup.getKind();
99
100  switch (Kind) {
101  default:
102    llvm_unreachable("invalid fixup kind!");
103  case FK_Data_4:
104    Type = ELF::R_MIPS_32;
105    break;
106  case FK_Data_8:
107    Type = ELF::R_MIPS_64;
108    break;
109  case FK_GPRel_4:
110    Type = ELF::R_MIPS_GPREL32;
111    break;
112  case Mips::fixup_Mips_GPREL16:
113    Type = ELF::R_MIPS_GPREL16;
114    break;
115  case Mips::fixup_Mips_26:
116    Type = ELF::R_MIPS_26;
117    break;
118  case Mips::fixup_Mips_CALL16:
119    Type = ELF::R_MIPS_CALL16;
120    break;
121  case Mips::fixup_Mips_GOT_Global:
122  case Mips::fixup_Mips_GOT_Local:
123    Type = ELF::R_MIPS_GOT16;
124    break;
125  case Mips::fixup_Mips_HI16:
126    Type = ELF::R_MIPS_HI16;
127    break;
128  case Mips::fixup_Mips_LO16:
129    Type = ELF::R_MIPS_LO16;
130    break;
131  case Mips::fixup_Mips_TLSGD:
132    Type = ELF::R_MIPS_TLS_GD;
133    break;
134  case Mips::fixup_Mips_GOTTPREL:
135    Type = ELF::R_MIPS_TLS_GOTTPREL;
136    break;
137  case Mips::fixup_Mips_TPREL_HI:
138    Type = ELF::R_MIPS_TLS_TPREL_HI16;
139    break;
140  case Mips::fixup_Mips_TPREL_LO:
141    Type = ELF::R_MIPS_TLS_TPREL_LO16;
142    break;
143  case Mips::fixup_Mips_TLSLDM:
144    Type = ELF::R_MIPS_TLS_LDM;
145    break;
146  case Mips::fixup_Mips_DTPREL_HI:
147    Type = ELF::R_MIPS_TLS_DTPREL_HI16;
148    break;
149  case Mips::fixup_Mips_DTPREL_LO:
150    Type = ELF::R_MIPS_TLS_DTPREL_LO16;
151    break;
152  case Mips::fixup_Mips_Branch_PCRel:
153  case Mips::fixup_Mips_PC16:
154    Type = ELF::R_MIPS_PC16;
155    break;
156  case Mips::fixup_Mips_GOT_PAGE:
157    Type = ELF::R_MIPS_GOT_PAGE;
158    break;
159  case Mips::fixup_Mips_GOT_OFST:
160    Type = ELF::R_MIPS_GOT_OFST;
161    break;
162  case Mips::fixup_Mips_GOT_DISP:
163    Type = ELF::R_MIPS_GOT_DISP;
164    break;
165  case Mips::fixup_Mips_GPOFF_HI:
166    Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
167    Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
168    Type = setRType3((unsigned)ELF::R_MIPS_HI16, Type);
169    break;
170  case Mips::fixup_Mips_GPOFF_LO:
171    Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
172    Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
173    Type = setRType3((unsigned)ELF::R_MIPS_LO16, Type);
174    break;
175  case Mips::fixup_Mips_HIGHER:
176    Type = ELF::R_MIPS_HIGHER;
177    break;
178  case Mips::fixup_Mips_HIGHEST:
179    Type = ELF::R_MIPS_HIGHEST;
180    break;
181  }
182  return Type;
183}
184
185// Return true if R is either a GOT16 against a local symbol or HI16.
186static bool NeedsMatchingLo(const MCAssembler &Asm, const RelEntry &R) {
187  if (!R.Sym)
188    return false;
189
190  MCSymbolData &SD = Asm.getSymbolData(R.Sym->AliasedSymbol());
191
192  return ((R.Reloc.Type == ELF::R_MIPS_GOT16) && !SD.isExternal()) ||
193    (R.Reloc.Type == ELF::R_MIPS_HI16);
194}
195
196static bool HasMatchingLo(const MCAssembler &Asm, RelLsIter I, RelLsIter Last) {
197  if (I == Last)
198    return false;
199
200  RelLsIter Hi = I++;
201
202  return (I->Reloc.Type == ELF::R_MIPS_LO16) && (Hi->Sym == I->Sym) &&
203    (Hi->Offset == I->Offset);
204}
205
206static bool HasSameSymbol(const RelEntry &R0, const RelEntry &R1) {
207  return R0.Sym == R1.Sym;
208}
209
210static int CompareOffset(const RelEntry &R0, const RelEntry &R1) {
211  return (R0.Offset > R1.Offset) ? 1 : ((R0.Offset == R1.Offset) ? 0 : -1);
212}
213
214void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,
215                                     std::vector<ELFRelocationEntry> &Relocs) {
216  // Call the default function first. Relocations are sorted in descending
217  // order of r_offset.
218  MCELFObjectTargetWriter::sortRelocs(Asm, Relocs);
219
220  RelLs RelocLs;
221  std::vector<RelLsIter> Unmatched;
222
223  // Fill RelocLs. Traverse Relocs backwards so that relocations in RelocLs
224  // are in ascending order of r_offset.
225  for (std::vector<ELFRelocationEntry>::reverse_iterator R = Relocs.rbegin();
226       R != Relocs.rend(); ++R) {
227     std::pair<const MCSymbolRefExpr*, int64_t> P =
228       MipsGetSymAndOffset(*R->Fixup);
229     RelocLs.push_back(RelEntry(*R, P.first ? &P.first->getSymbol() : 0,
230                                P.second));
231  }
232
233  // Get list of unmatched HI16 and GOT16.
234  for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
235    if (NeedsMatchingLo(Asm, *R) && !HasMatchingLo(Asm, R, --RelocLs.end()))
236      Unmatched.push_back(R);
237
238  // Insert unmatched HI16 and GOT16 immediately before their matching LO16.
239  for (std::vector<RelLsIter>::iterator U = Unmatched.begin();
240       U != Unmatched.end(); ++U) {
241    RelLsIter LoPos = RelocLs.end(), HiPos = *U;
242    bool MatchedLo = false;
243
244    for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R) {
245      if ((R->Reloc.Type == ELF::R_MIPS_LO16) && HasSameSymbol(*HiPos, *R) &&
246          (CompareOffset(*R, *HiPos) >= 0) &&
247          ((LoPos == RelocLs.end()) || ((CompareOffset(*R, *LoPos) < 0)) ||
248           (!MatchedLo && !CompareOffset(*R, *LoPos))))
249        LoPos = R;
250
251      MatchedLo = NeedsMatchingLo(Asm, *R) &&
252        HasMatchingLo(Asm, R, --RelocLs.end());
253    }
254
255    // If a matching LoPos was found, move HiPos and insert it before LoPos.
256    // Make the offsets of HiPos and LoPos match.
257    if (LoPos != RelocLs.end()) {
258      HiPos->Offset = LoPos->Offset;
259      RelocLs.insert(LoPos, *HiPos);
260      RelocLs.erase(HiPos);
261    }
262  }
263
264  // Put the sorted list back in reverse order.
265  assert(Relocs.size() == RelocLs.size());
266  unsigned I = RelocLs.size();
267
268  for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
269    Relocs[--I] = R->Reloc;
270}
271
272MCObjectWriter *llvm::createMipsELFObjectWriter(raw_ostream &OS,
273                                                uint8_t OSABI,
274                                                bool IsLittleEndian,
275                                                bool Is64Bit) {
276  MCELFObjectTargetWriter *MOTW = new MipsELFObjectWriter(Is64Bit, OSABI,
277                                                (Is64Bit) ? true : false);
278  return createELFObjectWriter(MOTW, OS, IsLittleEndian);
279}
280