Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp ---------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
///
10
/// \file For mach-o object files, this implementation converts from
11
/// mach-o on-disk binary format to in-memory normalized mach-o.
12
///
13
///                 +---------------+
14
///                 | binary mach-o |
15
///                 +---------------+
16
///                        |
17
///                        |
18
///                        v
19
///                  +------------+
20
///                  | normalized |
21
///                  +------------+
22
23
#include "ArchHandler.h"
24
#include "MachONormalizedFile.h"
25
#include "MachONormalizedFileBinaryUtils.h"
26
#include "lld/Common/LLVM.h"
27
#include "lld/Core/Error.h"
28
#include "lld/Core/SharedLibraryFile.h"
29
#include "llvm/ADT/STLExtras.h"
30
#include "llvm/ADT/SmallString.h"
31
#include "llvm/ADT/StringRef.h"
32
#include "llvm/ADT/StringSwitch.h"
33
#include "llvm/ADT/Twine.h"
34
#include "llvm/BinaryFormat/MachO.h"
35
#include "llvm/BinaryFormat/Magic.h"
36
#include "llvm/Object/MachO.h"
37
#include "llvm/Support/Casting.h"
38
#include "llvm/Support/Errc.h"
39
#include "llvm/Support/ErrorHandling.h"
40
#include "llvm/Support/FileOutputBuffer.h"
41
#include "llvm/Support/Host.h"
42
#include "llvm/Support/MemoryBuffer.h"
43
#include "llvm/Support/raw_ostream.h"
44
#include <functional>
45
#include <system_error>
46
47
using namespace llvm::MachO;
48
using llvm::object::ExportEntry;
49
using llvm::file_magic;
50
using llvm::object::MachOObjectFile;
51
52
namespace lld {
53
namespace mach_o {
54
namespace normalized {
55
56
// Utility to call a lambda expression on each load command.
57
static llvm::Error forEachLoadCommand(
58
    StringRef lcRange, unsigned lcCount, bool isBig, bool is64,
59
90
    std::function<bool(uint32_t cmd, uint32_t size, const char *lc)> func) {
60
90
  const char* p = lcRange.begin();
61
372
  for (unsigned i=0; i < lcCount; 
++i282
) {
62
298
    const load_command *lc = reinterpret_cast<const load_command*>(p);
63
298
    load_command lcCopy;
64
298
    const load_command *slc = lc;
65
298
    if (isBig != llvm::sys::IsBigEndianHost) {
66
12
      memcpy(&lcCopy, lc, sizeof(load_command));
67
12
      swapStruct(lcCopy);
68
12
      slc = &lcCopy;
69
12
    }
70
298
    if ( (p + slc->cmdsize) > lcRange.end() )
71
0
      return llvm::make_error<GenericError>("Load command exceeds range");
72
298
73
298
    if (func(slc->cmd, slc->cmdsize, p))
74
16
      return llvm::Error::success();
75
282
76
282
    p += slc->cmdsize;
77
282
  }
78
90
79
90
  
return llvm::Error::success()74
;
80
90
}
81
82
static std::error_code appendRelocations(Relocations &relocs, StringRef buffer,
83
                                         bool bigEndian,
84
86
                                         uint32_t reloff, uint32_t nreloc) {
85
86
  if ((reloff + nreloc*8) > buffer.size())
86
0
    return make_error_code(llvm::errc::executable_format_error);
87
86
  const any_relocation_info* relocsArray =
88
86
            reinterpret_cast<const any_relocation_info*>(buffer.begin()+reloff);
89
86
90
377
  for(uint32_t i=0; i < nreloc; 
++i291
) {
91
291
    relocs.push_back(unpackRelocation(relocsArray[i], bigEndian));
92
291
  }
93
86
  return std::error_code();
94
86
}
95
96
static std::error_code
97
appendIndirectSymbols(IndirectSymbols &isyms, StringRef buffer, bool isBig,
98
                      uint32_t istOffset, uint32_t istCount,
99
0
                      uint32_t startIndex, uint32_t count) {
100
0
  if ((istOffset + istCount*4) > buffer.size())
101
0
    return make_error_code(llvm::errc::executable_format_error);
102
0
  if (startIndex+count  > istCount)
103
0
    return make_error_code(llvm::errc::executable_format_error);
104
0
  const uint8_t *indirectSymbolArray = (const uint8_t *)buffer.data();
105
0
106
0
  for(uint32_t i=0; i < count; ++i) {
107
0
    isyms.push_back(read32(
108
0
        indirectSymbolArray + (startIndex + i) * sizeof(uint32_t), isBig));
109
0
  }
110
0
  return std::error_code();
111
0
}
112
113
114
421
template <typename T> static T readBigEndian(T t) {
115
421
  if (llvm::sys::IsLittleEndianHost)
116
421
    llvm::sys::swapByteOrder(t);
117
421
  return t;
118
421
}
119
120
121
46
static bool isMachOHeader(const mach_header *mh, bool &is64, bool &isBig) {
122
46
  switch (read32(&mh->magic, false)) {
123
46
  case llvm::MachO::MH_MAGIC:
124
15
    is64 = false;
125
15
    isBig = false;
126
15
    return true;
127
46
  case llvm::MachO::MH_MAGIC_64:
128
28
    is64 = true;
129
28
    isBig = false;
130
28
    return true;
131
46
  case llvm::MachO::MH_CIGAM:
132
3
    is64 = false;
133
3
    isBig = true;
134
3
    return true;
135
46
  case llvm::MachO::MH_CIGAM_64:
136
0
    is64 = true;
137
0
    isBig = true;
138
0
    return true;
139
46
  default:
140
0
    return false;
141
46
  }
142
46
}
143
144
145
1
bool isThinObjectFile(StringRef path, MachOLinkingContext::Arch &arch) {
146
1
  // Try opening and mapping file at path.
147
1
  ErrorOr<std::unique_ptr<MemoryBuffer>> b = MemoryBuffer::getFileOrSTDIN(path);
148
1
  if (b.getError())
149
0
    return false;
150
1
151
1
  // If file length < 32 it is too small to be mach-o object file.
152
1
  StringRef fileBuffer = b->get()->getBuffer();
153
1
  if (fileBuffer.size() < 32)
154
0
    return false;
155
1
156
1
  // If file buffer does not start with MH_MAGIC (and variants), not obj file.
157
1
  const mach_header *mh = reinterpret_cast<const mach_header *>(
158
1
                                                            fileBuffer.begin());
159
1
  bool is64, isBig;
160
1
  if (!isMachOHeader(mh, is64, isBig))
161
0
    return false;
162
1
163
1
  // If not MH_OBJECT, not object file.
164
1
  if (read32(&mh->filetype, isBig) != MH_OBJECT)
165
0
    return false;
166
1
167
1
  // Lookup up arch from cpu/subtype pair.
168
1
  arch = MachOLinkingContext::archFromCpuType(
169
1
      read32(&mh->cputype, isBig),
170
1
      read32(&mh->cpusubtype, isBig));
171
1
  return true;
172
1
}
173
174
bool sliceFromFatFile(MemoryBufferRef mb, MachOLinkingContext::Arch arch,
175
364
                      uint32_t &offset, uint32_t &size) {
176
364
  const char *start = mb.getBufferStart();
177
364
  const llvm::MachO::fat_header *fh =
178
364
      reinterpret_cast<const llvm::MachO::fat_header *>(start);
179
364
  if (readBigEndian(fh->magic) != llvm::MachO::FAT_MAGIC)
180
354
    return false;
181
10
  uint32_t nfat_arch = readBigEndian(fh->nfat_arch);
182
10
  const fat_arch *fstart =
183
10
      reinterpret_cast<const fat_arch *>(start + sizeof(fat_header));
184
10
  const fat_arch *fend =
185
10
      reinterpret_cast<const fat_arch *>(start + sizeof(fat_header) +
186
10
                                         sizeof(fat_arch) * nfat_arch);
187
10
  const uint32_t reqCpuType = MachOLinkingContext::cpuTypeFromArch(arch);
188
10
  const uint32_t reqCpuSubtype = MachOLinkingContext::cpuSubtypeFromArch(arch);
189
17
  for (const fat_arch *fa = fstart; fa < fend; 
++fa7
) {
190
17
    if ((readBigEndian(fa->cputype) == reqCpuType) &&
191
17
        
(readBigEndian(fa->cpusubtype) == reqCpuSubtype)10
) {
192
10
      offset = readBigEndian(fa->offset);
193
10
      size = readBigEndian(fa->size);
194
10
      if ((offset + size) > mb.getBufferSize())
195
0
        return false;
196
10
      return true;
197
10
    }
198
17
  }
199
10
  
return false0
;
200
10
}
201
202
/// Reads a mach-o file and produces an in-memory normalized view.
203
llvm::Expected<std::unique_ptr<NormalizedFile>>
204
readBinary(std::unique_ptr<MemoryBuffer> &mb,
205
45
           const MachOLinkingContext::Arch arch) {
206
45
  // Make empty NormalizedFile.
207
45
  std::unique_ptr<NormalizedFile> f(new NormalizedFile());
208
45
209
45
  const char *start = mb->getBufferStart();
210
45
  size_t objSize = mb->getBufferSize();
211
45
  const mach_header *mh = reinterpret_cast<const mach_header *>(start);
212
45
213
45
  uint32_t sliceOffset;
214
45
  uint32_t sliceSize;
215
45
  if (sliceFromFatFile(mb->getMemBufferRef(), arch, sliceOffset, sliceSize)) {
216
2
    start = &start[sliceOffset];
217
2
    objSize = sliceSize;
218
2
    mh = reinterpret_cast<const mach_header *>(start);
219
2
  }
220
45
221
45
  // Determine endianness and pointer size for mach-o file.
222
45
  bool is64, isBig;
223
45
  if (!isMachOHeader(mh, is64, isBig))
224
0
    return llvm::make_error<GenericError>("File is not a mach-o");
225
45
226
45
  // Endian swap header, if needed.
227
45
  mach_header headerCopy;
228
45
  const mach_header *smh = mh;
229
45
  if (isBig != llvm::sys::IsBigEndianHost) {
230
3
    memcpy(&headerCopy, mh, sizeof(mach_header));
231
3
    swapStruct(headerCopy);
232
3
    smh = &headerCopy;
233
3
  }
234
45
235
45
  // Validate head and load commands fit in buffer.
236
45
  const uint32_t lcCount = smh->ncmds;
237
45
  const char *lcStart =
238
45
      start + (is64 ? 
sizeof(mach_header_64)28
:
sizeof(mach_header)17
);
239
45
  StringRef lcRange(lcStart, smh->sizeofcmds);
240
45
  if (lcRange.end() > (start + objSize))
241
0
    return llvm::make_error<GenericError>("Load commands exceed file size");
242
45
243
45
  // Get architecture from mach_header.
244
45
  f->arch = MachOLinkingContext::archFromCpuType(smh->cputype, smh->cpusubtype);
245
45
  if (f->arch != arch) {
246
0
    return llvm::make_error<GenericError>(
247
0
                                  Twine("file is wrong architecture. Expected "
248
0
                                  "(" + MachOLinkingContext::nameFromArch(arch)
249
0
                                  + ") found ("
250
0
                                  + MachOLinkingContext::nameFromArch(f->arch)
251
0
                                  + ")" ));
252
0
  }
253
45
  // Copy file type and flags
254
45
  f->fileType = HeaderFileType(smh->filetype);
255
45
  f->flags = smh->flags;
256
45
257
45
258
45
  // Pre-scan load commands looking for indirect symbol table.
259
45
  uint32_t indirectSymbolTableOffset = 0;
260
45
  uint32_t indirectSymbolTableCount = 0;
261
45
  auto ec = forEachLoadCommand(lcRange, lcCount, isBig, is64,
262
45
                               [&](uint32_t cmd, uint32_t size,
263
138
                                   const char *lc) -> bool {
264
138
    if (cmd == LC_DYSYMTAB) {
265
16
      const dysymtab_command *d = reinterpret_cast<const dysymtab_command*>(lc);
266
16
      indirectSymbolTableOffset = read32(&d->indirectsymoff, isBig);
267
16
      indirectSymbolTableCount = read32(&d->nindirectsyms, isBig);
268
16
      return true;
269
16
    }
270
122
    return false;
271
122
  });
272
45
  if (ec)
273
0
    return std::move(ec);
274
45
275
45
  // Walk load commands looking for segments/sections and the symbol table.
276
45
  const data_in_code_entry *dataInCode = nullptr;
277
45
  const dyld_info_command *dyldInfo = nullptr;
278
45
  uint32_t dataInCodeSize = 0;
279
45
  ec = forEachLoadCommand(lcRange, lcCount, isBig, is64,
280
160
                    [&] (uint32_t cmd, uint32_t size, const char* lc) -> bool {
281
160
    switch(cmd) {
282
160
    case LC_SEGMENT_64:
283
33
      if (is64) {
284
33
        const segment_command_64 *seg =
285
33
                              reinterpret_cast<const segment_command_64*>(lc);
286
33
        const unsigned sectionCount = read32(&seg->nsects, isBig);
287
33
        const section_64 *sects = reinterpret_cast<const section_64*>
288
33
                                  (lc + sizeof(segment_command_64));
289
33
        const unsigned lcSize = sizeof(segment_command_64)
290
33
                                              + sectionCount*sizeof(section_64);
291
33
        // Verify sections don't extend beyond end of segment load command.
292
33
        if (lcSize > size)
293
0
          return true;
294
96
        
for (unsigned i=0; 33
i < sectionCount;
++i63
) {
295
63
          const section_64 *sect = &sects[i];
296
63
          Section section;
297
63
          section.segmentName = getString16(sect->segname);
298
63
          section.sectionName = getString16(sect->sectname);
299
63
          section.type = (SectionType)(read32(&sect->flags, isBig) &
300
63
                                       SECTION_TYPE);
301
63
          section.attributes  = read32(&sect->flags, isBig) & SECTION_ATTRIBUTES;
302
63
          section.alignment   = 1 << read32(&sect->align, isBig);
303
63
          section.address     = read64(&sect->addr, isBig);
304
63
          const uint8_t *content =
305
63
            (const uint8_t *)start + read32(&sect->offset, isBig);
306
63
          size_t contentSize = read64(&sect->size, isBig);
307
63
          // Note: this assign() is copying the content bytes.  Ideally,
308
63
          // we can use a custom allocator for vector to avoid the copy.
309
63
          section.content = llvm::makeArrayRef(content, contentSize);
310
63
          appendRelocations(section.relocations, mb->getBuffer(), isBig,
311
63
                            read32(&sect->reloff, isBig),
312
63
                            read32(&sect->nreloc, isBig));
313
63
          if (section.type == S_NON_LAZY_SYMBOL_POINTERS) {
314
0
            appendIndirectSymbols(section.indirectSymbols, mb->getBuffer(),
315
0
                                  isBig,
316
0
                                  indirectSymbolTableOffset,
317
0
                                  indirectSymbolTableCount,
318
0
                                  read32(&sect->reserved1, isBig),
319
0
                                  contentSize/4);
320
0
          }
321
63
          f->sections.push_back(section);
322
63
        }
323
33
      }
324
33
      break;
325
33
    case LC_SEGMENT:
326
17
      if (!is64) {
327
17
        const segment_command *seg =
328
17
                              reinterpret_cast<const segment_command*>(lc);
329
17
        const unsigned sectionCount = read32(&seg->nsects, isBig);
330
17
        const section *sects = reinterpret_cast<const section*>
331
17
                                  (lc + sizeof(segment_command));
332
17
        const unsigned lcSize = sizeof(segment_command)
333
17
                                              + sectionCount*sizeof(section);
334
17
        // Verify sections don't extend beyond end of segment load command.
335
17
        if (lcSize > size)
336
0
          return true;
337
40
        
for (unsigned i=0; 17
i < sectionCount;
++i23
) {
338
23
          const section *sect = &sects[i];
339
23
          Section section;
340
23
          section.segmentName = getString16(sect->segname);
341
23
          section.sectionName = getString16(sect->sectname);
342
23
          section.type = (SectionType)(read32(&sect->flags, isBig) &
343
23
                                       SECTION_TYPE);
344
23
          section.attributes =
345
23
              read32((const uint8_t *)&sect->flags, isBig) & SECTION_ATTRIBUTES;
346
23
          section.alignment   = 1 << read32(&sect->align, isBig);
347
23
          section.address     = read32(&sect->addr, isBig);
348
23
          const uint8_t *content =
349
23
            (const uint8_t *)start + read32(&sect->offset, isBig);
350
23
          size_t contentSize = read32(&sect->size, isBig);
351
23
          // Note: this assign() is copying the content bytes.  Ideally,
352
23
          // we can use a custom allocator for vector to avoid the copy.
353
23
          section.content = llvm::makeArrayRef(content, contentSize);
354
23
          appendRelocations(section.relocations, mb->getBuffer(), isBig,
355
23
                            read32(&sect->reloff, isBig),
356
23
                            read32(&sect->nreloc, isBig));
357
23
          if (section.type == S_NON_LAZY_SYMBOL_POINTERS) {
358
0
            appendIndirectSymbols(
359
0
                section.indirectSymbols, mb->getBuffer(), isBig,
360
0
                indirectSymbolTableOffset, indirectSymbolTableCount,
361
0
                read32(&sect->reserved1, isBig), contentSize / 4);
362
0
          }
363
23
          f->sections.push_back(section);
364
23
        }
365
17
      }
366
17
      break;
367
39
    case LC_SYMTAB: {
368
39
      const symtab_command *st = reinterpret_cast<const symtab_command*>(lc);
369
39
      const char *strings = start + read32(&st->stroff, isBig);
370
39
      const uint32_t strSize = read32(&st->strsize, isBig);
371
39
      // Validate string pool and symbol table all in buffer.
372
39
      if (read32((const uint8_t *)&st->stroff, isBig) +
373
39
              read32((const uint8_t *)&st->strsize, isBig) >
374
39
          objSize)
375
0
        return true;
376
39
      if (is64) {
377
26
        const uint32_t symOffset = read32(&st->symoff, isBig);
378
26
        const uint32_t symCount = read32(&st->nsyms, isBig);
379
26
        if ( symOffset+(symCount*sizeof(nlist_64)) > objSize)
380
0
          return true;
381
26
        const nlist_64 *symbols =
382
26
            reinterpret_cast<const nlist_64 *>(start + symOffset);
383
26
        // Convert each nlist_64 to a lld::mach_o::normalized::Symbol.
384
99
        for(uint32_t i=0; i < symCount; 
++i73
) {
385
73
          nlist_64 tempSym;
386
73
          memcpy(&tempSym, &symbols[i], sizeof(nlist_64));
387
73
          const nlist_64 *sin = &tempSym;
388
73
          if (isBig != llvm::sys::IsBigEndianHost)
389
0
            swapStruct(tempSym);
390
73
          Symbol sout;
391
73
          if (sin->n_strx > strSize)
392
0
            return true;
393
73
          sout.name  = &strings[sin->n_strx];
394
73
          sout.type = static_cast<NListType>(sin->n_type & (N_STAB|N_TYPE));
395
73
          sout.scope = (sin->n_type & (N_PEXT|N_EXT));
396
73
          sout.sect  = sin->n_sect;
397
73
          sout.desc  = sin->n_desc;
398
73
          sout.value = sin->n_value;
399
73
          if (sin->n_type & N_STAB)
400
0
            f->stabsSymbols.push_back(sout);
401
73
          else if (sout.type == N_UNDF)
402
20
            f->undefinedSymbols.push_back(sout);
403
53
          else if (sin->n_type & N_EXT)
404
30
            f->globalSymbols.push_back(sout);
405
23
          else
406
23
            f->localSymbols.push_back(sout);
407
73
        }
408
26
      } else {
409
13
        const uint32_t symOffset = read32(&st->symoff, isBig);
410
13
        const uint32_t symCount = read32(&st->nsyms, isBig);
411
13
        if ( symOffset+(symCount*sizeof(nlist)) > objSize)
412
0
          return true;
413
13
        const nlist *symbols =
414
13
            reinterpret_cast<const nlist *>(start + symOffset);
415
13
        // Convert each nlist to a lld::mach_o::normalized::Symbol.
416
52
        for(uint32_t i=0; i < symCount; 
++i39
) {
417
39
          const nlist *sin = &symbols[i];
418
39
          nlist tempSym;
419
39
          if (isBig != llvm::sys::IsBigEndianHost) {
420
5
            tempSym = *sin; swapStruct(tempSym); sin = &tempSym;
421
5
          }
422
39
          Symbol sout;
423
39
          if (sin->n_strx > strSize)
424
0
            return true;
425
39
          sout.name  = &strings[sin->n_strx];
426
39
          sout.type  = (NListType)(sin->n_type & N_TYPE);
427
39
          sout.scope = (sin->n_type & (N_PEXT|N_EXT));
428
39
          sout.sect  = sin->n_sect;
429
39
          sout.desc  = sin->n_desc;
430
39
          sout.value = sin->n_value;
431
39
          if (sout.type == N_UNDF)
432
9
            f->undefinedSymbols.push_back(sout);
433
30
          else if (sout.scope == (SymbolScope)N_EXT)
434
12
            f->globalSymbols.push_back(sout);
435
18
          else if (sin->n_type & N_STAB)
436
0
            f->stabsSymbols.push_back(sout);
437
18
          else
438
18
            f->localSymbols.push_back(sout);
439
39
        }
440
13
      }
441
39
      }
442
39
      break;
443
39
    case LC_ID_DYLIB: {
444
4
      const dylib_command *dl = reinterpret_cast<const dylib_command*>(lc);
445
4
      f->installName = lc + read32(&dl->dylib.name, isBig);
446
4
      f->currentVersion = read32(&dl->dylib.current_version, isBig);
447
4
      f->compatVersion = read32(&dl->dylib.compatibility_version, isBig);
448
4
      }
449
4
      break;
450
39
    case LC_DATA_IN_CODE: {
451
23
      const linkedit_data_command *ldc =
452
23
                            reinterpret_cast<const linkedit_data_command*>(lc);
453
23
      dataInCode = reinterpret_cast<const data_in_code_entry *>(
454
23
          start + read32(&ldc->dataoff, isBig));
455
23
      dataInCodeSize = read32(&ldc->datasize, isBig);
456
23
      }
457
23
      break;
458
39
    case LC_LOAD_DYLIB:
459
4
    case LC_LOAD_WEAK_DYLIB:
460
4
    case LC_REEXPORT_DYLIB:
461
4
    case LC_LOAD_UPWARD_DYLIB: {
462
4
      const dylib_command *dl = reinterpret_cast<const dylib_command*>(lc);
463
4
      DependentDylib entry;
464
4
      entry.path = lc + read32(&dl->dylib.name, isBig);
465
4
      entry.kind = LoadCommandType(cmd);
466
4
      entry.compatVersion = read32(&dl->dylib.compatibility_version, isBig);
467
4
      entry.currentVersion = read32(&dl->dylib.current_version, isBig);
468
4
      f->dependentDylibs.push_back(entry);
469
4
     }
470
4
      break;
471
4
    case LC_RPATH: {
472
0
      const rpath_command *rpc = reinterpret_cast<const rpath_command *>(lc);
473
0
      f->rpaths.push_back(lc + read32(&rpc->path, isBig));
474
0
     }
475
0
      break;
476
4
    case LC_DYLD_INFO:
477
4
    case LC_DYLD_INFO_ONLY:
478
4
      dyldInfo = reinterpret_cast<const dyld_info_command*>(lc);
479
4
      break;
480
10
    case LC_VERSION_MIN_MACOSX:
481
10
    case LC_VERSION_MIN_IPHONEOS:
482
10
    case LC_VERSION_MIN_WATCHOS:
483
10
    case LC_VERSION_MIN_TVOS:
484
10
      // If we are emitting an object file, then we may take the load command
485
10
      // kind from these commands and pass it on to the output
486
10
      // file.
487
10
      f->minOSVersionKind = (LoadCommandType)cmd;
488
10
      break;
489
160
    }
490
160
    return false;
491
160
  });
492
45
  if (ec)
493
0
    return std::move(ec);
494
45
495
45
  if (dataInCode) {
496
23
    // Convert on-disk data_in_code_entry array to DataInCode vector.
497
33
    for (unsigned i=0; i < dataInCodeSize/sizeof(data_in_code_entry); 
++i10
) {
498
10
      DataInCode entry;
499
10
      entry.offset = read32(&dataInCode[i].offset, isBig);
500
10
      entry.length = read16(&dataInCode[i].length, isBig);
501
10
      entry.kind =
502
10
          (DataRegionType)read16((const uint8_t *)&dataInCode[i].kind, isBig);
503
10
      f->dataInCode.push_back(entry);
504
10
    }
505
23
  }
506
45
507
45
  if (dyldInfo) {
508
4
    // If any exports, extract and add to normalized exportInfo vector.
509
4
    if (dyldInfo->export_size) {
510
4
      const uint8_t *trieStart = reinterpret_cast<const uint8_t *>(
511
4
          start + read32(&dyldInfo->export_off, isBig));
512
4
      ArrayRef<uint8_t> trie(trieStart, read32(&dyldInfo->export_size, isBig));
513
4
      Error Err = Error::success();
514
4
      for (const ExportEntry &trieExport : MachOObjectFile::exports(Err, trie)) {
515
4
        Export normExport;
516
4
        normExport.name = trieExport.name().copy(f->ownedAllocations);
517
4
        normExport.offset = trieExport.address();
518
4
        normExport.kind = ExportSymbolKind(trieExport.flags() & EXPORT_SYMBOL_FLAGS_KIND_MASK);
519
4
        normExport.flags = trieExport.flags() & ~EXPORT_SYMBOL_FLAGS_KIND_MASK;
520
4
        normExport.otherOffset = trieExport.other();
521
4
        if (!trieExport.otherName().empty())
522
0
          normExport.otherName = trieExport.otherName().copy(f->ownedAllocations);
523
4
        f->exportInfo.push_back(normExport);
524
4
      }
525
4
      if (Err)
526
0
        return std::move(Err);
527
45
    }
528
4
  }
529
45
530
45
  return std::move(f);
531
45
}
532
533
class MachOObjectReader : public Reader {
534
public:
535
225
  MachOObjectReader(MachOLinkingContext &ctx) : _ctx(ctx) {}
536
537
325
  bool canParse(file_magic magic, MemoryBufferRef mb) const override {
538
325
    return (magic == file_magic::macho_object && 
mb.getBufferSize() > 3227
);
539
325
  }
540
541
  ErrorOr<std::unique_ptr<File>>
542
  loadFile(std::unique_ptr<MemoryBuffer> mb,
543
27
           const Registry &registry) const override {
544
27
    std::unique_ptr<File> ret =
545
27
      llvm::make_unique<MachOFile>(std::move(mb), &_ctx);
546
27
    return std::move(ret);
547
27
  }
548
549
private:
550
  MachOLinkingContext &_ctx;
551
};
552
553
class MachODylibReader : public Reader {
554
public:
555
225
  MachODylibReader(MachOLinkingContext &ctx) : _ctx(ctx) {}
556
557
298
  bool canParse(file_magic magic, MemoryBufferRef mb) const override {
558
298
    switch (magic) {
559
298
    case file_magic::macho_dynamically_linked_shared_lib:
560
4
    case file_magic::macho_dynamically_linked_shared_lib_stub:
561
4
      return mb.getBufferSize() > 32;
562
294
    default:
563
294
      return false;
564
298
    }
565
298
  }
566
567
  ErrorOr<std::unique_ptr<File>>
568
  loadFile(std::unique_ptr<MemoryBuffer> mb,
569
4
           const Registry &registry) const override {
570
4
    std::unique_ptr<File> ret =
571
4
        llvm::make_unique<MachODylibFile>(std::move(mb), &_ctx);
572
4
    return std::move(ret);
573
4
  }
574
575
private:
576
  MachOLinkingContext &_ctx;
577
};
578
579
} // namespace normalized
580
} // namespace mach_o
581
582
225
void Registry::addSupportMachOObjects(MachOLinkingContext &ctx) {
583
225
  MachOLinkingContext::Arch arch = ctx.arch();
584
225
  add(std::unique_ptr<Reader>(new mach_o::normalized::MachOObjectReader(ctx)));
585
225
  add(std::unique_ptr<Reader>(new mach_o::normalized::MachODylibReader(ctx)));
586
225
  addKindTable(Reference::KindNamespace::mach_o, ctx.archHandler().kindArch(),
587
225
               ctx.archHandler().kindStrings());
588
225
  add(std::unique_ptr<YamlIOTaggedDocumentHandler>(
589
225
                           new mach_o::MachOYamlIOTaggedDocumentHandler(arch)));
590
225
}
591
592
593
} // namespace lld