summaryrefslogtreecommitdiffstats
path: root/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp
blob: 91e3aa71611838413bc625aacf283d08e0173c9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//===- lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp ---------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

///
/// \file For mach-o object files, this implementation converts from
/// mach-o on-disk binary format to in-memory normalized mach-o.
///
///                 +---------------+
///                 | binary mach-o |
///                 +---------------+
///                        |
///                        |
///                        v
///                  +------------+
///                  | normalized |
///                  +------------+

#include "MachONormalizedFile.h"
#include "MachONormalizedFileBinaryUtils.h"

#include "lld/Core/Error.h"
#include "lld/Core/LLVM.h"

#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileOutputBuffer.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/MachO.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/system_error.h"

#include <functional>

using namespace llvm::MachO;

namespace lld {
namespace mach_o {
namespace normalized {

// Utility to call a lambda expression on each load command.
static error_code 
forEachLoadCommand(StringRef lcRange, unsigned lcCount, bool swap, bool is64,
                   std::function<bool (uint32_t cmd, uint32_t size, 
                                                      const char* lc)> func) {
  const char* p = lcRange.begin();
  for (unsigned i=0; i < lcCount; ++i) {
    const load_command *lc = reinterpret_cast<const load_command*>(p);
    load_command lcCopy;
    const load_command *slc = lc;
    if (swap) {
      memcpy(&lcCopy, lc, sizeof(load_command));
      swapStruct(lcCopy);
      slc = &lcCopy;
    }
    if ( (p + slc->cmdsize) > lcRange.end() )
      return llvm::make_error_code(llvm::errc::executable_format_error);
  
    if (func(slc->cmd, slc->cmdsize, p))
      return error_code::success();
  
    p += slc->cmdsize;
  } 
  
  return error_code::success();
}


static error_code 
appendRelocations(Relocations &relocs, StringRef buffer, bool swap, 
                             bool bigEndian, uint32_t reloff, uint32_t nreloc) {
  if ((reloff + nreloc*8) > buffer.size())
    return llvm::make_error_code(llvm::errc::executable_format_error);
  const any_relocation_info* relocsArray = 
            reinterpret_cast<const any_relocation_info*>(buffer.begin()+reloff); 
  
  for(uint32_t i=0; i < nreloc; ++i) {
    relocs.push_back(unpackRelocation(relocsArray[i], swap, bigEndian));
  }
  return error_code::success();
}



/// Reads a mach-o file and produces an in-memory normalized view.
ErrorOr<std::unique_ptr<NormalizedFile>> 
readBinary(std::unique_ptr<MemoryBuffer> &mb) {
  // Make empty NormalizedFile.
  std::unique_ptr<NormalizedFile> f(new NormalizedFile());

  // Determine endianness and pointer size for mach-o file.
  const mach_header *mh = reinterpret_cast<const mach_header*>
                                                      (mb->getBufferStart());
  bool is64, swap;
  switch (mh->magic) {
  case llvm::MachO::MH_MAGIC:
    is64 = false;
    swap = false;
    break;
  case llvm::MachO::MH_MAGIC_64:
    is64 = true;
    swap = false;
    break;
  case llvm::MachO::MH_CIGAM:
    is64 = false;
    swap = true;
    break;
  case llvm::MachO::MH_CIGAM_64:
    is64 = true;
    swap = true;
    break;
  default:
    return llvm::make_error_code(llvm::errc::executable_format_error);
  }

  // Endian swap header, if needed.
  mach_header headerCopy;
  const mach_header *smh = mh;
  if (swap) {
    memcpy(&headerCopy, mh, sizeof(mach_header));
    swapStruct(headerCopy);
    smh = &headerCopy;
  }

  // Validate head and load commands fit in buffer.
  const uint32_t lcCount = smh->ncmds;
  const char* lcStart = mb->getBufferStart() + (is64 ? sizeof(mach_header_64) 
                                                     : sizeof(mach_header));
  StringRef lcRange(lcStart, smh->sizeofcmds);
  if (lcRange.end() > mb->getBufferEnd())
    return llvm::make_error_code(llvm::errc::executable_format_error);

  // Normalize architecture
  f->arch = MachOLinkingContext::archFromCpuType(smh->cputype, smh->cpusubtype);
  bool isBigEndianArch = MachOLinkingContext::isBigEndian(f->arch);
  // Copy file type and flags
  f->fileType = HeaderFileType(smh->filetype);
  f->flags = smh->flags;


  // Walk load commands looking for segments/sections and the symbol table.
  error_code ec = forEachLoadCommand(lcRange, lcCount, swap, is64, 
                    [&] (uint32_t cmd, uint32_t size, const char* lc) -> bool {
    if (is64) {
      if (cmd == LC_SEGMENT_64) {
        const segment_command_64 *seg = 
                              reinterpret_cast<const segment_command_64*>(lc);
        const unsigned sectionCount = (swap ? SwapByteOrder(seg->nsects)
                                            : seg->nsects);
        const section_64 *sects = reinterpret_cast<const section_64*>
                                  (lc + sizeof(segment_command_64));
        const unsigned lcSize = sizeof(segment_command_64) 
                                              + sectionCount*sizeof(section_64);
        // Verify sections don't extend beyond end of segment load command.
        if (lcSize > size) 
          return llvm::make_error_code(llvm::errc::executable_format_error);
        for (unsigned i=0; i < sectionCount; ++i) {
          const section_64 *sect = &sects[i];
          Section section;
          section.segmentName = getString16(sect->segname);
          section.sectionName = getString16(sect->sectname);
          section.type        = (SectionType)(read32(swap, sect->flags) 
                                                                & SECTION_TYPE);
          section.attributes  = read32(swap, sect->flags) & SECTION_ATTRIBUTES;
          section.alignment   = read32(swap, sect->align);
          section.address     = read64(swap, sect->addr);
          const char *content = mb->getBufferStart() 
                                           + read32(swap, sect->offset);
          size_t contentSize = read64(swap, sect->size);
          // Note: this assign() is copying the content bytes.  Ideally,
          // we can use a custom allocator for vector to avoid the copy.
          section.content.assign(content, content+contentSize);
          appendRelocations(section.relocations, mb->getBuffer(), 
                            swap, isBigEndianArch, read32(swap, sect->reloff), 
                                                   read32(swap, sect->nreloc));
          f->sections.push_back(section);
        }
      }
    } else {
      if (cmd == LC_SEGMENT) {
        const segment_command *seg = 
                              reinterpret_cast<const segment_command*>(lc);
        const unsigned sectionCount = (swap ? SwapByteOrder(seg->nsects)
                                            : seg->nsects);
        const section *sects = reinterpret_cast<const section*>
                                  (lc + sizeof(segment_command));
        const unsigned lcSize = sizeof(segment_command) 
                                              + sectionCount*sizeof(section);
        // Verify sections don't extend beyond end of segment load command.
        if (lcSize > size) 
          return llvm::make_error_code(llvm::errc::executable_format_error);
        for (unsigned i=0; i < sectionCount; ++i) {
          const section *sect = &sects[i];
          Section section;
          section.segmentName = getString16(sect->segname);
          section.sectionName = getString16(sect->sectname);
          section.type        = (SectionType)(read32(swap, sect->flags) 
                                                                & SECTION_TYPE);
          section.attributes  = read32(swap, sect->flags) & SECTION_ATTRIBUTES;
          section.alignment   = read32(swap, sect->align);
          section.address     = read32(swap, sect->addr);
          const char *content = mb->getBufferStart() 
                                           + read32(swap, sect->offset);
          size_t contentSize = read32(swap, sect->size);
          // Note: this assign() is copying the content bytes.  Ideally,
          // we can use a custom allocator for vector to avoid the copy.
          section.content.assign(content, content+contentSize);
          appendRelocations(section.relocations, mb->getBuffer(), 
                            swap, isBigEndianArch, read32(swap, sect->reloff), 
                                                   read32(swap, sect->nreloc));
          f->sections.push_back(section);
        }
      }
    }
    if (cmd == LC_SYMTAB) {
      const symtab_command *st = reinterpret_cast<const symtab_command*>(lc);
      const char* strings = mb->getBufferStart() + read32(swap, st->stroff);
      const uint32_t strSize = read32(swap, st->strsize);
      // Validate string pool and symbol table all in buffer.
      if ( read32(swap, st->stroff)+read32(swap, st->strsize) 
                                                        > mb->getBufferSize() )
        return llvm::make_error_code(llvm::errc::executable_format_error);
      if (is64) {
        const uint32_t symOffset = read32(swap, st->symoff);
        const uint32_t symCount = read32(swap, st->nsyms);
        if ( symOffset+(symCount*sizeof(nlist_64)) > mb->getBufferSize())
          return llvm::make_error_code(llvm::errc::executable_format_error);
        const nlist_64* symbols = reinterpret_cast<const nlist_64*> 
                                            (mb->getBufferStart() + symOffset);
        // Convert each nlist_64 to a lld::mach_o::normalized::Symbol.
        for(uint32_t i=0; i < symCount; ++i) {
          const nlist_64 *sin = &symbols[i];
          nlist_64 tempSym;
          if (swap) {
            tempSym = *sin; swapStruct(tempSym); sin = &tempSym;
          }
          Symbol sout;
          if (sin->n_strx > strSize) 
            return llvm::make_error_code(llvm::errc::executable_format_error);
          sout.name  = &strings[sin->n_strx];
          sout.type  = (NListType)(sin->n_type & N_TYPE);
          sout.scope = (sin->n_type & (N_PEXT|N_EXT));
          sout.sect  = sin->n_sect;
          sout.desc  = sin->n_desc;
          sout.value = sin->n_value;
          if (sout.type == N_UNDF)
            f->undefinedSymbols.push_back(sout);
          else if (sout.scope == (SymbolScope)N_EXT)
            f->globalSymbols.push_back(sout);
          else
            f->localSymbols.push_back(sout);
        }
      } else { 
        const uint32_t symOffset = read32(swap, st->symoff);
        const uint32_t symCount = read32(swap, st->nsyms);
        if ( symOffset+(symCount*sizeof(nlist)) > mb->getBufferSize())
          return llvm::make_error_code(llvm::errc::executable_format_error);
        const nlist* symbols = reinterpret_cast<const nlist*> 
                                            (mb->getBufferStart() + symOffset);
        // Convert each nlist to a lld::mach_o::normalized::Symbol.
        for(uint32_t i=0; i < symCount; ++i) {
          const nlist *sin = &symbols[i];
          nlist tempSym;
          if (swap) {
            tempSym = *sin; swapStruct(tempSym); sin = &tempSym;
          }
          Symbol sout;
          if (sin->n_strx > strSize) 
            return llvm::make_error_code(llvm::errc::executable_format_error);
          sout.name  = &strings[sin->n_strx];
          sout.type  = (NListType)(sin->n_type & N_TYPE);
          sout.scope = (sin->n_type & (N_PEXT|N_EXT));
          sout.sect  = sin->n_sect;
          sout.desc  = sin->n_desc;
          sout.value = sin->n_value;
          if (sout.type == N_UNDF)
            f->undefinedSymbols.push_back(sout);
          else if (sout.scope == (SymbolScope)N_EXT)
            f->globalSymbols.push_back(sout);
          else
            f->localSymbols.push_back(sout);
        }
      }
    } else if (cmd == LC_DYSYMTAB) {
      // TODO: indirect symbols 
    }

    return false;
  });
  if (ec) 
    return ec;

  return std::move(f);
}


} // namespace normalized
} // namespace mach_o
} // namespace lld

OpenPOWER on IntegriCloud