Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Object/ObjectFile.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- ObjectFile.cpp - File format independent object file ---------------===//
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
// This file defines a file format independent ObjectFile class.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/Object/ObjectFile.h"
14
#include "llvm/ADT/StringRef.h"
15
#include "llvm/BinaryFormat/Magic.h"
16
#include "llvm/Object/Binary.h"
17
#include "llvm/Object/COFF.h"
18
#include "llvm/Object/Error.h"
19
#include "llvm/Object/MachO.h"
20
#include "llvm/Object/Wasm.h"
21
#include "llvm/Support/Error.h"
22
#include "llvm/Support/ErrorHandling.h"
23
#include "llvm/Support/ErrorOr.h"
24
#include "llvm/Support/FileSystem.h"
25
#include "llvm/Support/MemoryBuffer.h"
26
#include "llvm/Support/raw_ostream.h"
27
#include <algorithm>
28
#include <cstdint>
29
#include <memory>
30
#include <system_error>
31
32
using namespace llvm;
33
using namespace object;
34
35
0
void ObjectFile::anchor() {}
36
37
ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source)
38
30.3k
    : SymbolicFile(Type, Source) {}
39
40
1.61k
bool SectionRef::containsSymbol(SymbolRef S) const {
41
1.61k
  Expected<section_iterator> SymSec = S.getSection();
42
1.61k
  if (!SymSec) {
43
0
    // TODO: Actually report errors helpfully.
44
0
    consumeError(SymSec.takeError());
45
0
    return false;
46
0
  }
47
1.61k
  return *this == **SymSec;
48
1.61k
}
49
50
35.7k
uint64_t ObjectFile::getSymbolValue(DataRefImpl Ref) const {
51
35.7k
  uint32_t Flags = getSymbolFlags(Ref);
52
35.7k
  if (Flags & SymbolRef::SF_Undefined)
53
1.37k
    return 0;
54
34.4k
  if (Flags & SymbolRef::SF_Common)
55
111
    return getCommonSymbolSize(Ref);
56
34.3k
  return getSymbolValueImpl(Ref);
57
34.3k
}
58
59
2.81k
Error ObjectFile::printSymbolName(raw_ostream &OS, DataRefImpl Symb) const {
60
2.81k
  Expected<StringRef> Name = getSymbolName(Symb);
61
2.81k
  if (!Name)
62
2
    return Name.takeError();
63
2.81k
  OS << *Name;
64
2.81k
  return Error::success();
65
2.81k
}
66
67
0
uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; }
68
69
67.5k
bool ObjectFile::isSectionBitcode(DataRefImpl Sec) const {
70
67.5k
  if (Expected<StringRef> NameOrErr = getSectionName(Sec))
71
67.5k
    return *NameOrErr == ".llvmbc";
72
0
  return false;
73
0
}
74
75
10.8k
bool ObjectFile::isSectionStripped(DataRefImpl Sec) const { return false; }
76
77
0
bool ObjectFile::isBerkeleyText(DataRefImpl Sec) const {
78
0
  return isSectionText(Sec);
79
0
}
80
81
0
bool ObjectFile::isBerkeleyData(DataRefImpl Sec) const {
82
0
  return isSectionData(Sec);
83
0
}
84
85
12.3k
section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
86
12.3k
  return section_iterator(SectionRef(Sec, this));
87
12.3k
}
88
89
2.20k
Triple ObjectFile::makeTriple() const {
90
2.20k
  Triple TheTriple;
91
2.20k
  auto Arch = getArch();
92
2.20k
  TheTriple.setArch(Triple::ArchType(Arch));
93
2.20k
94
2.20k
  // For ARM targets, try to use the build attributes to build determine
95
2.20k
  // the build target. Target features are also added, but later during
96
2.20k
  // disassembly.
97
2.20k
  if (Arch == Triple::arm || 
Arch == Triple::armeb2.15k
)
98
53
    setARMSubArch(TheTriple);
99
2.20k
100
2.20k
  // TheTriple defaults to ELF, and COFF doesn't have an environment:
101
2.20k
  // the best we can do here is indicate that it is mach-o.
102
2.20k
  if (isMachO())
103
24
    TheTriple.setObjectFormat(Triple::MachO);
104
2.20k
105
2.20k
  if (isCOFF()) {
106
68
    const auto COFFObj = dyn_cast<COFFObjectFile>(this);
107
68
    if (COFFObj->getArch() == Triple::thumb)
108
21
      TheTriple.setTriple("thumbv7-windows");
109
68
  }
110
2.20k
111
2.20k
  return TheTriple;
112
2.20k
}
113
114
Expected<std::unique_ptr<ObjectFile>>
115
14.2k
ObjectFile::createObjectFile(MemoryBufferRef Object, file_magic Type) {
116
14.2k
  StringRef Data = Object.getBuffer();
117
14.2k
  if (Type == file_magic::unknown)
118
1.65k
    Type = identify_magic(Data);
119
14.2k
120
14.2k
  switch (Type) {
121
14.2k
  case file_magic::unknown:
122
164
  case file_magic::bitcode:
123
164
  case file_magic::coff_cl_gl_object:
124
164
  case file_magic::archive:
125
164
  case file_magic::macho_universal_binary:
126
164
  case file_magic::windows_resource:
127
164
  case file_magic::pdb:
128
164
  case file_magic::minidump:
129
164
    return errorCodeToError(object_error::invalid_file_type);
130
8.07k
  case file_magic::elf:
131
8.07k
  case file_magic::elf_relocatable:
132
8.07k
  case file_magic::elf_executable:
133
8.07k
  case file_magic::elf_shared_object:
134
8.07k
  case file_magic::elf_core:
135
8.07k
    return createELFObjectFile(Object);
136
8.07k
  case file_magic::macho_object:
137
2.64k
  case file_magic::macho_executable:
138
2.64k
  case file_magic::macho_fixed_virtual_memory_shared_lib:
139
2.64k
  case file_magic::macho_core:
140
2.64k
  case file_magic::macho_preload_executable:
141
2.64k
  case file_magic::macho_dynamically_linked_shared_lib:
142
2.64k
  case file_magic::macho_dynamic_linker:
143
2.64k
  case file_magic::macho_bundle:
144
2.64k
  case file_magic::macho_dynamically_linked_shared_lib_stub:
145
2.64k
  case file_magic::macho_dsym_companion:
146
2.64k
  case file_magic::macho_kext_bundle:
147
2.64k
    return createMachOObjectFile(Object);
148
2.64k
  case file_magic::coff_object:
149
2.51k
  case file_magic::coff_import_library:
150
2.51k
  case file_magic::pecoff_executable:
151
2.51k
    return createCOFFObjectFile(Object);
152
2.51k
  case file_magic::xcoff_object_32:
153
10
    return createXCOFFObjectFile(Object, Binary::ID_XCOFF32);
154
2.51k
  case file_magic::xcoff_object_64:
155
2
    return createXCOFFObjectFile(Object, Binary::ID_XCOFF64);
156
2.51k
  case file_magic::wasm_object:
157
844
    return createWasmObjectFile(Object);
158
0
  }
159
0
  llvm_unreachable("Unexpected Object File Type");
160
0
}
161
162
Expected<OwningBinary<ObjectFile>>
163
114
ObjectFile::createObjectFile(StringRef ObjectPath) {
164
114
  ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
165
114
      MemoryBuffer::getFile(ObjectPath);
166
114
  if (std::error_code EC = FileOrErr.getError())
167
13
    return errorCodeToError(EC);
168
101
  std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
169
101
170
101
  Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
171
101
      createObjectFile(Buffer->getMemBufferRef());
172
101
  if (Error Err = ObjOrErr.takeError())
173
35
    return std::move(Err);
174
66
  std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
175
66
176
66
  return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));
177
66
}