LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MipsELFObjectWriter.cpp
Go to the documentation of this file.
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 
13 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCSection.h"
17 #include "llvm/MC/MCValue.h"
19 #include <list>
20 
21 using namespace llvm;
22 
23 namespace {
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,
38  bool _isN64, bool IsLittleEndian);
39 
40  virtual ~MipsELFObjectWriter();
41 
42  virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
43  bool IsPCRel, bool IsRelocWithSymbol,
44  int64_t Addend) 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 
55 MipsELFObjectWriter::MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI,
56  bool _isN64, bool IsLittleEndian)
57  : MCELFObjectTargetWriter(_is64Bit, OSABI, ELF::EM_MIPS,
58  /*HasRelocationAddend*/ (_isN64) ? true : false,
59  /*IsN64*/ _isN64) {}
60 
61 MipsELFObjectWriter::~MipsELFObjectWriter() {}
62 
63 const MCSymbol *MipsELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
64  const MCValue &Target,
65  const MCFragment &F,
66  const MCFixup &Fixup,
67  bool IsPCRel) const {
68  assert(Target.getSymA() && "SymA cannot be 0.");
69  const MCSymbol &Sym = Target.getSymA()->getSymbol().AliasedSymbol();
70 
71  if (Sym.getSection().getKind().isMergeableCString() ||
72  Sym.getSection().getKind().isMergeableConst())
73  return &Sym;
74 
75  return NULL;
76 }
77 
78 unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target,
79  const MCFixup &Fixup,
80  bool IsPCRel,
81  bool IsRelocWithSymbol,
82  int64_t Addend) const {
83  // determine the type of the relocation
84  unsigned Type = (unsigned)ELF::R_MIPS_NONE;
85  unsigned Kind = (unsigned)Fixup.getKind();
86 
87  switch (Kind) {
88  default:
89  llvm_unreachable("invalid fixup kind!");
90  case FK_Data_4:
91  Type = ELF::R_MIPS_32;
92  break;
93  case FK_Data_8:
94  Type = ELF::R_MIPS_64;
95  break;
96  case FK_GPRel_4:
97  if (isN64()) {
98  Type = setRType((unsigned)ELF::R_MIPS_GPREL32, Type);
99  Type = setRType2((unsigned)ELF::R_MIPS_64, Type);
100  Type = setRType3((unsigned)ELF::R_MIPS_NONE, Type);
101  }
102  else
103  Type = ELF::R_MIPS_GPREL32;
104  break;
106  Type = ELF::R_MIPS_GPREL16;
107  break;
108  case Mips::fixup_Mips_26:
109  Type = ELF::R_MIPS_26;
110  break;
112  Type = ELF::R_MIPS_CALL16;
113  break;
116  Type = ELF::R_MIPS_GOT16;
117  break;
119  Type = ELF::R_MIPS_HI16;
120  break;
122  Type = ELF::R_MIPS_LO16;
123  break;
125  Type = ELF::R_MIPS_TLS_GD;
126  break;
129  break;
132  break;
135  break;
137  Type = ELF::R_MIPS_TLS_LDM;
138  break;
141  break;
144  break;
147  Type = ELF::R_MIPS_PC16;
148  break;
150  Type = ELF::R_MIPS_GOT_PAGE;
151  break;
153  Type = ELF::R_MIPS_GOT_OFST;
154  break;
156  Type = ELF::R_MIPS_GOT_DISP;
157  break;
159  Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
160  Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
161  Type = setRType3((unsigned)ELF::R_MIPS_HI16, Type);
162  break;
164  Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
165  Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
166  Type = setRType3((unsigned)ELF::R_MIPS_LO16, Type);
167  break;
169  Type = ELF::R_MIPS_HIGHER;
170  break;
172  Type = ELF::R_MIPS_HIGHEST;
173  break;
175  Type = ELF::R_MIPS_GOT_HI16;
176  break;
178  Type = ELF::R_MIPS_GOT_LO16;
179  break;
181  Type = ELF::R_MIPS_CALL_HI16;
182  break;
184  Type = ELF::R_MIPS_CALL_LO16;
185  break;
187  Type = ELF::R_MICROMIPS_26_S1;
188  break;
190  Type = ELF::R_MICROMIPS_HI16;
191  break;
193  Type = ELF::R_MICROMIPS_LO16;
194  break;
196  Type = ELF::R_MICROMIPS_GOT16;
197  break;
200  break;
203  break;
206  break;
209  break;
212  break;
215  break;
218  break;
221  break;
224  break;
225  }
226  return Type;
227 }
228 
229 // Return true if R is either a GOT16 against a local symbol or HI16.
230 static bool NeedsMatchingLo(const MCAssembler &Asm, const RelEntry &R) {
231  if (!R.Sym)
232  return false;
233 
234  MCSymbolData &SD = Asm.getSymbolData(R.Sym->AliasedSymbol());
235 
236  return ((R.Reloc.Type == ELF::R_MIPS_GOT16) && !SD.isExternal()) ||
237  (R.Reloc.Type == ELF::R_MIPS_HI16);
238 }
239 
240 static bool HasMatchingLo(const MCAssembler &Asm, RelLsIter I, RelLsIter Last) {
241  if (I == Last)
242  return false;
243 
244  RelLsIter Hi = I++;
245 
246  return (I->Reloc.Type == ELF::R_MIPS_LO16) && (Hi->Sym == I->Sym) &&
247  (Hi->Offset == I->Offset);
248 }
249 
250 static bool HasSameSymbol(const RelEntry &R0, const RelEntry &R1) {
251  return R0.Sym == R1.Sym;
252 }
253 
254 static int CompareOffset(const RelEntry &R0, const RelEntry &R1) {
255  return (R0.Offset > R1.Offset) ? 1 : ((R0.Offset == R1.Offset) ? 0 : -1);
256 }
257 
258 void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,
259  std::vector<ELFRelocationEntry> &Relocs) {
260  // Call the default function first. Relocations are sorted in descending
261  // order of r_offset.
262  MCELFObjectTargetWriter::sortRelocs(Asm, Relocs);
263 
264  RelLs RelocLs;
265  std::vector<RelLsIter> Unmatched;
266 
267  // Fill RelocLs. Traverse Relocs backwards so that relocations in RelocLs
268  // are in ascending order of r_offset.
269  for (std::vector<ELFRelocationEntry>::reverse_iterator R = Relocs.rbegin();
270  R != Relocs.rend(); ++R) {
271  std::pair<const MCSymbolRefExpr*, int64_t> P =
272  MipsGetSymAndOffset(*R->Fixup);
273  RelocLs.push_back(RelEntry(*R, P.first ? &P.first->getSymbol() : 0,
274  P.second));
275  }
276 
277  // Get list of unmatched HI16 and GOT16.
278  for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
279  if (NeedsMatchingLo(Asm, *R) && !HasMatchingLo(Asm, R, --RelocLs.end()))
280  Unmatched.push_back(R);
281 
282  // Insert unmatched HI16 and GOT16 immediately before their matching LO16.
283  for (std::vector<RelLsIter>::iterator U = Unmatched.begin();
284  U != Unmatched.end(); ++U) {
285  RelLsIter LoPos = RelocLs.end(), HiPos = *U;
286  bool MatchedLo = false;
287 
288  for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R) {
289  if ((R->Reloc.Type == ELF::R_MIPS_LO16) && HasSameSymbol(*HiPos, *R) &&
290  (CompareOffset(*R, *HiPos) >= 0) &&
291  ((LoPos == RelocLs.end()) || ((CompareOffset(*R, *LoPos) < 0)) ||
292  (!MatchedLo && !CompareOffset(*R, *LoPos))))
293  LoPos = R;
294 
295  MatchedLo = NeedsMatchingLo(Asm, *R) &&
296  HasMatchingLo(Asm, R, --RelocLs.end());
297  }
298 
299  // If a matching LoPos was found, move HiPos and insert it before LoPos.
300  // Make the offsets of HiPos and LoPos match.
301  if (LoPos != RelocLs.end()) {
302  HiPos->Offset = LoPos->Offset;
303  RelocLs.insert(LoPos, *HiPos);
304  RelocLs.erase(HiPos);
305  }
306  }
307 
308  // Put the sorted list back in reverse order.
309  assert(Relocs.size() == RelocLs.size());
310  unsigned I = RelocLs.size();
311 
312  for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
313  Relocs[--I] = R->Reloc;
314 }
315 
317  uint8_t OSABI,
318  bool IsLittleEndian,
319  bool Is64Bit) {
320  MCELFObjectTargetWriter *MOTW = new MipsELFObjectWriter(Is64Bit, OSABI,
321  (Is64Bit) ? true : false,
322  IsLittleEndian);
323  return createELFObjectWriter(MOTW, OS, IsLittleEndian);
324 }
COFF::RelocationTypeX86 Type
Definition: COFFYAML.cpp:227
const MCSymbol & getSymbol() const
Definition: MCExpr.h:283
F(f)
#define llvm_unreachable(msg)
#define false
Definition: ConvertUTF.c:64
static bool NeedsMatchingLo(const MCAssembler &Asm, const RelEntry &R)
A four-byte fixup.
Definition: MCFixup.h:25
A four-byte gp relative fixup.
Definition: MCFixup.h:33
MCObjectWriter * createMipsELFObjectWriter(raw_ostream &OS, uint8_t OSABI, bool IsLittleEndian, bool Is64Bit)
bool isExternal() const
Definition: MCAssembler.h:730
#define P(N)
#define true
Definition: ConvertUTF.c:65
static bool HasMatchingLo(const MCAssembler &Asm, RelLsIter I, RelLsIter Last)
MCFixupKind getKind() const
Definition: MCFixup.h:88
MCObjectWriter * createELFObjectWriter(MCELFObjectTargetWriter *MOTW, raw_ostream &OS, bool IsLittleEndian)
Construct a new ELF writer instance.
static int CompareOffset(const RelEntry &R0, const RelEntry &R1)
MCSymbolData & getSymbolData(const MCSymbol &Symbol) const
Definition: MCAssembler.h:1145
static std::pair< const MCSymbolRefExpr *, int64_t > MipsGetSymAndOffset(const MCFixup &Fixup)
Definition: MipsBaseInfo.h:125
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:43
const MCSymbol & AliasedSymbol() const
Definition: MCSymbol.cpp:42
std::reverse_iterator< const_iterator > reverse_iterator
Definition: Path.h:79
#define I(x, y, z)
Definition: MD5.cpp:54
A eight-byte fixup.
Definition: MCFixup.h:26
static bool HasSameSymbol(const RelEntry &R0, const RelEntry &R1)