Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 1 | //===- Archive.cpp - ar File Format implementation --------------*- C++ -*-===// |
| 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 | // This file defines the ArchiveObjectFile class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Object/Archive.h" |
| 15 | #include "llvm/ADT/APInt.h" |
Michael J. Spencer | c8a55a6 | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Endian.h" |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 17 | #include "llvm/Support/MemoryBuffer.h" |
| 18 | |
| 19 | using namespace llvm; |
| 20 | using namespace object; |
| 21 | |
Benjamin Kramer | e1a4427 | 2012-02-22 13:42:11 +0000 | [diff] [blame] | 22 | static const char *Magic = "!<arch>\n"; |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 23 | |
Michael J. Spencer | aaf98ea | 2011-10-25 22:30:42 +0000 | [diff] [blame] | 24 | static bool isInternalMember(const ArchiveMemberHeader &amh) { |
Craig Topper | e329810 | 2012-05-24 06:35:32 +0000 | [diff] [blame] | 25 | static const char *const internals[] = { |
Michael J. Spencer | aaf98ea | 2011-10-25 22:30:42 +0000 | [diff] [blame] | 26 | "/", |
| 27 | "//", |
| 28 | "#_LLVM_SYM_TAB_#" |
Craig Topper | e329810 | 2012-05-24 06:35:32 +0000 | [diff] [blame] | 29 | }; |
Michael J. Spencer | aaf98ea | 2011-10-25 22:30:42 +0000 | [diff] [blame] | 30 | |
| 31 | StringRef name = amh.getName(); |
| 32 | for (std::size_t i = 0; i < sizeof(internals) / sizeof(*internals); ++i) { |
| 33 | if (name == internals[i]) |
| 34 | return true; |
| 35 | } |
| 36 | return false; |
| 37 | } |
| 38 | |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 39 | void Archive::anchor() { } |
| 40 | |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 41 | error_code Archive::Child::getName(StringRef &Result) const { |
| 42 | StringRef name = ToHeader(Data.data())->getName(); |
| 43 | // Check if it's a special name. |
| 44 | if (name[0] == '/') { |
| 45 | if (name.size() == 1) { // Linker member. |
| 46 | Result = name; |
| 47 | return object_error::success; |
| 48 | } |
| 49 | if (name.size() == 2 && name[1] == '/') { // String table. |
| 50 | Result = name; |
| 51 | return object_error::success; |
| 52 | } |
| 53 | // It's a long name. |
| 54 | // Get the offset. |
Michael J. Spencer | bf82b07 | 2013-01-10 00:07:38 +0000 | [diff] [blame] | 55 | std::size_t offset; |
Michael J. Spencer | 7932c41 | 2013-01-10 01:05:34 +0000 | [diff] [blame] | 56 | if (name.substr(1).rtrim(" ").getAsInteger(10, offset)) |
| 57 | llvm_unreachable("Long name offset is not an integer"); |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 58 | const char *addr = Parent->StringTable->Data.begin() |
| 59 | + sizeof(ArchiveMemberHeader) |
Michael J. Spencer | e92800d | 2013-01-09 22:58:43 +0000 | [diff] [blame] | 60 | + offset; |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 61 | // Verify it. |
| 62 | if (Parent->StringTable == Parent->end_children() |
| 63 | || addr < (Parent->StringTable->Data.begin() |
| 64 | + sizeof(ArchiveMemberHeader)) |
| 65 | || addr > (Parent->StringTable->Data.begin() |
| 66 | + sizeof(ArchiveMemberHeader) |
| 67 | + Parent->StringTable->getSize())) |
| 68 | return object_error::parse_failed; |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 69 | |
| 70 | // GNU long file names end with a /. |
| 71 | if (Parent->kind() == K_GNU) { |
| 72 | StringRef::size_type End = StringRef(addr).find('/'); |
| 73 | Result = StringRef(addr, End); |
| 74 | } else { |
| 75 | Result = addr; |
| 76 | } |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 77 | return object_error::success; |
Michael J. Spencer | aaf98ea | 2011-10-25 22:30:42 +0000 | [diff] [blame] | 78 | } else if (name.startswith("#1/")) { |
Michael J. Spencer | e92800d | 2013-01-09 22:58:43 +0000 | [diff] [blame] | 79 | uint64_t name_size; |
Michael J. Spencer | 7932c41 | 2013-01-10 01:05:34 +0000 | [diff] [blame] | 80 | if (name.substr(3).rtrim(" ").getAsInteger(10, name_size)) |
| 81 | llvm_unreachable("Long name length is not an ingeter"); |
| 82 | Result = Data.substr(sizeof(ArchiveMemberHeader), name_size); |
Michael J. Spencer | aaf98ea | 2011-10-25 22:30:42 +0000 | [diff] [blame] | 83 | return object_error::success; |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 84 | } |
| 85 | // It's a simple name. |
| 86 | if (name[name.size() - 1] == '/') |
| 87 | Result = name.substr(0, name.size() - 1); |
| 88 | else |
| 89 | Result = name; |
| 90 | return object_error::success; |
| 91 | } |
| 92 | |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 93 | error_code Archive::Child::getAsBinary(OwningPtr<Binary> &Result) const { |
| 94 | OwningPtr<Binary> ret; |
Michael J. Spencer | 0f76e64 | 2013-02-03 10:48:50 +0000 | [diff] [blame] | 95 | OwningPtr<MemoryBuffer> Buff; |
| 96 | if (error_code ec = getMemoryBuffer(Buff)) |
| 97 | return ec; |
| 98 | if (error_code ec = createBinary(Buff.take(), ret)) |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 99 | return ec; |
| 100 | Result.swap(ret); |
| 101 | return object_error::success; |
| 102 | } |
| 103 | |
| 104 | Archive::Archive(MemoryBuffer *source, error_code &ec) |
David Meyer | 6f9489a | 2012-03-09 20:41:57 +0000 | [diff] [blame] | 105 | : Binary(Binary::ID_Archive, source) { |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 106 | // Check for sufficient magic. |
| 107 | if (!source || source->getBufferSize() |
| 108 | < (8 + sizeof(ArchiveMemberHeader) + 2) // Smallest archive. |
| 109 | || StringRef(source->getBufferStart(), 8) != Magic) { |
| 110 | ec = object_error::invalid_file_type; |
| 111 | return; |
| 112 | } |
| 113 | |
Michael J. Spencer | c8a55a6 | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 114 | // Get the special members. |
| 115 | child_iterator i = begin_children(false); |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 116 | child_iterator e = end_children(); |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 117 | |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 118 | StringRef name; |
| 119 | if ((ec = i->getName(name))) |
| 120 | return; |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 121 | |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 122 | // Below is the pattern that is used to figure out the archive format |
| 123 | // GNU archive format |
| 124 | // First member : / (points to the symbol table ) |
| 125 | // Second member : // (may exist, if it exists, points to the string table) |
| 126 | // Note : The string table is used if the filename exceeds 15 characters |
| 127 | // BSD archive format |
| 128 | // First member : __.SYMDEF (points to the symbol table) |
| 129 | // There is no string table, if the filename exceeds 15 characters or has a |
| 130 | // embedded space, the filename has #1/<size>, The size represents the size |
| 131 | // of the filename that needs to be read after the archive header |
| 132 | // COFF archive format |
| 133 | // First member : / |
| 134 | // Second member : / (provides a directory of symbols) |
| 135 | // Third member : // contains the string table, this is present even if the |
| 136 | // string table is empty |
| 137 | if (name == "/") { |
| 138 | SymbolTable = i; |
| 139 | StringTable = e; |
| 140 | if (i != e) ++i; |
Michael J. Spencer | bf82b07 | 2013-01-10 00:07:38 +0000 | [diff] [blame] | 141 | if (i == e) { |
| 142 | ec = object_error::parse_failed; |
| 143 | return; |
| 144 | } |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 145 | if ((ec = i->getName(name))) |
| 146 | return; |
| 147 | if (name[0] != '/') { |
| 148 | Format = K_GNU; |
| 149 | } else if ((name.size() > 1) && (name == "//")) { |
| 150 | Format = K_GNU; |
| 151 | StringTable = i; |
| 152 | ++i; |
| 153 | } else { |
| 154 | Format = K_COFF; |
| 155 | if (i != e) { |
| 156 | SymbolTable = i; |
| 157 | ++i; |
| 158 | } |
| 159 | if (i != e) { |
| 160 | StringTable = i; |
| 161 | } |
| 162 | } |
| 163 | } else if (name == "__.SYMDEF") { |
| 164 | Format = K_BSD; |
| 165 | SymbolTable = i; |
| 166 | StringTable = e; |
| 167 | } |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 168 | ec = object_error::success; |
| 169 | } |
| 170 | |
Michael J. Spencer | aaf98ea | 2011-10-25 22:30:42 +0000 | [diff] [blame] | 171 | Archive::child_iterator Archive::begin_children(bool skip_internal) const { |
Benjamin Kramer | e1a4427 | 2012-02-22 13:42:11 +0000 | [diff] [blame] | 172 | const char *Loc = Data->getBufferStart() + strlen(Magic); |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 173 | size_t Size = sizeof(ArchiveMemberHeader) + |
| 174 | ToHeader(Loc)->getSize(); |
Michael J. Spencer | aaf98ea | 2011-10-25 22:30:42 +0000 | [diff] [blame] | 175 | Child c(this, StringRef(Loc, Size)); |
| 176 | // Skip internals at the beginning of an archive. |
| 177 | if (skip_internal && isInternalMember(*ToHeader(Loc))) |
| 178 | return c.getNext(); |
| 179 | return c; |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Michael J. Spencer | 5861893 | 2011-10-08 00:17:45 +0000 | [diff] [blame] | 182 | Archive::child_iterator Archive::end_children() const { |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 183 | return Child(this, StringRef(0, 0)); |
| 184 | } |
Michael J. Spencer | c8a55a6 | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 185 | |
| 186 | error_code Archive::Symbol::getName(StringRef &Result) const { |
Michael J. Spencer | 0f76e64 | 2013-02-03 10:48:50 +0000 | [diff] [blame] | 187 | Result = StringRef(Parent->SymbolTable->getBuffer().begin() + StringIndex); |
Michael J. Spencer | c8a55a6 | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 188 | return object_error::success; |
| 189 | } |
| 190 | |
| 191 | error_code Archive::Symbol::getMember(child_iterator &Result) const { |
Michael J. Spencer | 0f76e64 | 2013-02-03 10:48:50 +0000 | [diff] [blame] | 192 | const char *Buf = Parent->SymbolTable->getBuffer().begin(); |
Michael J. Spencer | 768a707 | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 193 | const char *Offsets = Buf + 4; |
| 194 | uint32_t Offset = 0; |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 195 | if (Parent->kind() == K_GNU) { |
Michael J. Spencer | 768a707 | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 196 | Offset = *(reinterpret_cast<const support::ubig32_t*>(Offsets) |
| 197 | + SymbolIndex); |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 198 | } else if (Parent->kind() == K_BSD) { |
Matt Beaumont-Gay | 7af4b9b | 2012-11-14 17:58:11 +0000 | [diff] [blame] | 199 | llvm_unreachable("BSD format is not supported"); |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 200 | } else { |
Michael J. Spencer | 768a707 | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 201 | uint32_t MemberCount = *reinterpret_cast<const support::ulittle32_t*>(Buf); |
| 202 | |
| 203 | // Skip offsets. |
| 204 | Buf += sizeof(support::ulittle32_t) |
| 205 | + (MemberCount * sizeof(support::ulittle32_t)); |
| 206 | |
| 207 | uint32_t SymbolCount = *reinterpret_cast<const support::ulittle32_t*>(Buf); |
| 208 | |
| 209 | if (SymbolIndex >= SymbolCount) |
| 210 | return object_error::parse_failed; |
| 211 | |
Matt Beaumont-Gay | f1c2a6b | 2012-11-14 00:21:27 +0000 | [diff] [blame] | 212 | // Skip SymbolCount to get to the indices table. |
| 213 | const char *Indices = Buf + sizeof(support::ulittle32_t); |
Michael J. Spencer | 768a707 | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 214 | |
| 215 | // Get the index of the offset in the file member offset table for this |
| 216 | // symbol. |
| 217 | uint16_t OffsetIndex = |
Matt Beaumont-Gay | f1c2a6b | 2012-11-14 00:21:27 +0000 | [diff] [blame] | 218 | *(reinterpret_cast<const support::ulittle16_t*>(Indices) |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 219 | + SymbolIndex); |
Michael J. Spencer | 768a707 | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 220 | // Subtract 1 since OffsetIndex is 1 based. |
| 221 | --OffsetIndex; |
| 222 | |
| 223 | if (OffsetIndex >= MemberCount) |
| 224 | return object_error::parse_failed; |
| 225 | |
| 226 | Offset = *(reinterpret_cast<const support::ulittle32_t*>(Offsets) |
| 227 | + OffsetIndex); |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 228 | } |
Michael J. Spencer | c8a55a6 | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 229 | |
Michael J. Spencer | 768a707 | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 230 | const char *Loc = Parent->getData().begin() + Offset; |
Michael J. Spencer | c8a55a6 | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 231 | size_t Size = sizeof(ArchiveMemberHeader) + |
| 232 | ToHeader(Loc)->getSize(); |
| 233 | Result = Child(Parent, StringRef(Loc, Size)); |
| 234 | |
| 235 | return object_error::success; |
| 236 | } |
| 237 | |
| 238 | Archive::Symbol Archive::Symbol::getNext() const { |
| 239 | Symbol t(*this); |
Benjamin Kramer | efd2d5e | 2011-11-04 13:52:17 +0000 | [diff] [blame] | 240 | // Go to one past next null. |
| 241 | t.StringIndex = |
Michael J. Spencer | 0f76e64 | 2013-02-03 10:48:50 +0000 | [diff] [blame] | 242 | Parent->SymbolTable->getBuffer().find('\0', t.StringIndex) + 1; |
Michael J. Spencer | c8a55a6 | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 243 | ++t.SymbolIndex; |
| 244 | return t; |
| 245 | } |
| 246 | |
| 247 | Archive::symbol_iterator Archive::begin_symbols() const { |
Michael J. Spencer | 0f76e64 | 2013-02-03 10:48:50 +0000 | [diff] [blame] | 248 | const char *buf = SymbolTable->getBuffer().begin(); |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 249 | if (kind() == K_GNU) { |
| 250 | uint32_t symbol_count = 0; |
| 251 | symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf); |
| 252 | buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t))); |
| 253 | } else if (kind() == K_BSD) { |
Matt Beaumont-Gay | 7af4b9b | 2012-11-14 17:58:11 +0000 | [diff] [blame] | 254 | llvm_unreachable("BSD archive format is not supported"); |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 255 | } else { |
| 256 | uint32_t member_count = 0; |
| 257 | uint32_t symbol_count = 0; |
| 258 | member_count = *reinterpret_cast<const support::ulittle32_t*>(buf); |
| 259 | buf += 4 + (member_count * 4); // Skip offsets. |
| 260 | symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf); |
| 261 | buf += 4 + (symbol_count * 2); // Skip indices. |
| 262 | } |
Michael J. Spencer | 0f76e64 | 2013-02-03 10:48:50 +0000 | [diff] [blame] | 263 | uint32_t string_start_offset = buf - SymbolTable->getBuffer().begin(); |
Michael J. Spencer | c8a55a6 | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 264 | return symbol_iterator(Symbol(this, 0, string_start_offset)); |
| 265 | } |
| 266 | |
| 267 | Archive::symbol_iterator Archive::end_symbols() const { |
Michael J. Spencer | 0f76e64 | 2013-02-03 10:48:50 +0000 | [diff] [blame] | 268 | const char *buf = SymbolTable->getBuffer().begin(); |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 269 | uint32_t symbol_count = 0; |
| 270 | if (kind() == K_GNU) { |
| 271 | symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf); |
| 272 | buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t))); |
| 273 | } else if (kind() == K_BSD) { |
Matt Beaumont-Gay | 7af4b9b | 2012-11-14 17:58:11 +0000 | [diff] [blame] | 274 | llvm_unreachable("BSD archive format is not supported"); |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 275 | } else { |
| 276 | uint32_t member_count = 0; |
| 277 | member_count = *reinterpret_cast<const support::ulittle32_t*>(buf); |
| 278 | buf += 4 + (member_count * 4); // Skip offsets. |
| 279 | symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf); |
| 280 | } |
Michael J. Spencer | c8a55a6 | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 281 | return symbol_iterator( |
| 282 | Symbol(this, symbol_count, 0)); |
| 283 | } |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 284 | |
| 285 | Archive::child_iterator Archive::findSym(StringRef name) const { |
| 286 | Archive::symbol_iterator bs = begin_symbols(); |
| 287 | Archive::symbol_iterator es = end_symbols(); |
| 288 | Archive::child_iterator result; |
| 289 | |
| 290 | StringRef symname; |
| 291 | for (; bs != es; ++bs) { |
| 292 | if (bs->getName(symname)) |
| 293 | return end_children(); |
| 294 | if (symname == name) { |
| 295 | if (bs->getMember(result)) |
| 296 | return end_children(); |
| 297 | return result; |
| 298 | } |
| 299 | } |
| 300 | return end_children(); |
| 301 | } |