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" 16 #include "llvm/ADT/SmallString.h" 17 #include "llvm/ADT/Twine.h" 18 #include "llvm/Support/Endian.h" 19 #include "llvm/Support/MemoryBuffer.h" 20 21 using namespace llvm; 22 using namespace object; 23 24 static const char *const Magic = "!<arch>\n"; 25 26 void Archive::anchor() { } 27 28 StringRef ArchiveMemberHeader::getName() const { 29 char EndCond; 30 if (Name[0] == '/' || Name[0] == '#') 31 EndCond = ' '; 32 else 33 EndCond = '/'; 34 llvm::StringRef::size_type end = 35 llvm::StringRef(Name, sizeof(Name)).find(EndCond); 36 if (end == llvm::StringRef::npos) 37 end = sizeof(Name); 38 assert(end <= sizeof(Name) && end > 0); 39 // Don't include the EndCond if there is one. 40 return llvm::StringRef(Name, end); 41 } 42 43 uint32_t ArchiveMemberHeader::getSize() const { 44 uint32_t Ret; 45 if (llvm::StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, Ret)) 46 llvm_unreachable("Size is not a decimal number."); 47 return Ret; 48 } 49 50 sys::fs::perms ArchiveMemberHeader::getAccessMode() const { 51 unsigned Ret; 52 if (StringRef(AccessMode, sizeof(AccessMode)).rtrim(" ").getAsInteger(8, Ret)) 53 llvm_unreachable("Access mode is not an octal number."); 54 return static_cast<sys::fs::perms>(Ret); 55 } 56 57 sys::TimeValue ArchiveMemberHeader::getLastModified() const { 58 unsigned Seconds; 59 if (StringRef(LastModified, sizeof(LastModified)).rtrim(" ") 60 .getAsInteger(10, Seconds)) 61 llvm_unreachable("Last modified time not a decimal number."); 62 63 sys::TimeValue Ret; 64 Ret.fromEpochTime(Seconds); 65 return Ret; 66 } 67 68 unsigned ArchiveMemberHeader::getUID() const { 69 unsigned Ret; 70 if (StringRef(UID, sizeof(UID)).rtrim(" ").getAsInteger(10, Ret)) 71 llvm_unreachable("UID time not a decimal number."); 72 return Ret; 73 } 74 75 unsigned ArchiveMemberHeader::getGID() const { 76 unsigned Ret; 77 if (StringRef(GID, sizeof(GID)).rtrim(" ").getAsInteger(10, Ret)) 78 llvm_unreachable("GID time not a decimal number."); 79 return Ret; 80 } 81 82 Archive::Child::Child(const Archive *Parent, const char *Start) 83 : Parent(Parent) { 84 if (!Start) 85 return; 86 87 const ArchiveMemberHeader *Header = 88 reinterpret_cast<const ArchiveMemberHeader *>(Start); 89 Data = StringRef(Start, sizeof(ArchiveMemberHeader) + Header->getSize()); 90 91 // Setup StartOfFile and PaddingBytes. 92 StartOfFile = sizeof(ArchiveMemberHeader); 93 // Don't include attached name. 94 StringRef Name = Header->getName(); 95 if (Name.startswith("#1/")) { 96 uint64_t NameSize; 97 if (Name.substr(3).rtrim(" ").getAsInteger(10, NameSize)) 98 llvm_unreachable("Long name length is not an integer"); 99 StartOfFile += NameSize; 100 } 101 } 102 103 Archive::Child Archive::Child::getNext() const { 104 size_t SpaceToSkip = Data.size(); 105 // If it's odd, add 1 to make it even. 106 if (SpaceToSkip & 1) 107 ++SpaceToSkip; 108 109 const char *NextLoc = Data.data() + SpaceToSkip; 110 111 // Check to see if this is past the end of the archive. 112 if (NextLoc >= Parent->Data->getBufferEnd()) 113 return Child(Parent, NULL); 114 115 return Child(Parent, NextLoc); 116 } 117 118 error_code Archive::Child::getName(StringRef &Result) const { 119 StringRef name = getRawName(); 120 // Check if it's a special name. 121 if (name[0] == '/') { 122 if (name.size() == 1) { // Linker member. 123 Result = name; 124 return object_error::success; 125 } 126 if (name.size() == 2 && name[1] == '/') { // String table. 127 Result = name; 128 return object_error::success; 129 } 130 // It's a long name. 131 // Get the offset. 132 std::size_t offset; 133 if (name.substr(1).rtrim(" ").getAsInteger(10, offset)) 134 llvm_unreachable("Long name offset is not an integer"); 135 const char *addr = Parent->StringTable->Data.begin() 136 + sizeof(ArchiveMemberHeader) 137 + offset; 138 // Verify it. 139 if (Parent->StringTable == Parent->child_end() 140 || addr < (Parent->StringTable->Data.begin() 141 + sizeof(ArchiveMemberHeader)) 142 || addr > (Parent->StringTable->Data.begin() 143 + sizeof(ArchiveMemberHeader) 144 + Parent->StringTable->getSize())) 145 return object_error::parse_failed; 146 147 // GNU long file names end with a /. 148 if (Parent->kind() == K_GNU) { 149 StringRef::size_type End = StringRef(addr).find('/'); 150 Result = StringRef(addr, End); 151 } else { 152 Result = addr; 153 } 154 return object_error::success; 155 } else if (name.startswith("#1/")) { 156 uint64_t name_size; 157 if (name.substr(3).rtrim(" ").getAsInteger(10, name_size)) 158 llvm_unreachable("Long name length is not an ingeter"); 159 Result = Data.substr(sizeof(ArchiveMemberHeader), name_size) 160 .rtrim(StringRef("\0", 1)); 161 return object_error::success; 162 } 163 // It's a simple name. 164 if (name[name.size() - 1] == '/') 165 Result = name.substr(0, name.size() - 1); 166 else 167 Result = name; 168 return object_error::success; 169 } 170 171 error_code Archive::Child::getMemoryBuffer(OwningPtr<MemoryBuffer> &Result, 172 bool FullPath) const { 173 StringRef Name; 174 if (error_code ec = getName(Name)) 175 return ec; 176 SmallString<128> Path; 177 Result.reset(MemoryBuffer::getMemBuffer( 178 getBuffer(), FullPath ? (Twine(Parent->getFileName()) + "(" + Name + ")") 179 .toStringRef(Path) 180 : Name, 181 false)); 182 return error_code::success(); 183 } 184 185 error_code Archive::Child::getAsBinary(OwningPtr<Binary> &Result) const { 186 OwningPtr<Binary> ret; 187 OwningPtr<MemoryBuffer> Buff; 188 if (error_code ec = getMemoryBuffer(Buff)) 189 return ec; 190 ErrorOr<Binary *> BinaryOrErr = createBinary(Buff.take()); 191 if (error_code EC = BinaryOrErr.getError()) 192 return EC; 193 Result.reset(BinaryOrErr.get()); 194 return object_error::success; 195 } 196 197 ErrorOr<Archive*> Archive::create(MemoryBuffer *Source) { 198 error_code EC; 199 OwningPtr<Archive> Ret(new Archive(Source, EC)); 200 if (EC) 201 return EC; 202 return Ret.take(); 203 } 204 205 Archive::Archive(MemoryBuffer *source, error_code &ec) 206 : Binary(Binary::ID_Archive, source), SymbolTable(child_end()) { 207 // Check for sufficient magic. 208 assert(source); 209 if (source->getBufferSize() < 8 || 210 StringRef(source->getBufferStart(), 8) != Magic) { 211 ec = object_error::invalid_file_type; 212 return; 213 } 214 215 // Get the special members. 216 child_iterator i = child_begin(false); 217 child_iterator e = child_end(); 218 219 if (i == e) { 220 ec = object_error::success; 221 return; 222 } 223 224 StringRef Name = i->getRawName(); 225 226 // Below is the pattern that is used to figure out the archive format 227 // GNU archive format 228 // First member : / (may exist, if it exists, points to the symbol table ) 229 // Second member : // (may exist, if it exists, points to the string table) 230 // Note : The string table is used if the filename exceeds 15 characters 231 // BSD archive format 232 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table) 233 // There is no string table, if the filename exceeds 15 characters or has a 234 // embedded space, the filename has #1/<size>, The size represents the size 235 // of the filename that needs to be read after the archive header 236 // COFF archive format 237 // First member : / 238 // Second member : / (provides a directory of symbols) 239 // Third member : // (may exist, if it exists, contains the string table) 240 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present 241 // even if the string table is empty. However, lib.exe does not in fact 242 // seem to create the third member if there's no member whose filename 243 // exceeds 15 characters. So the third member is optional. 244 245 if (Name == "__.SYMDEF") { 246 Format = K_BSD; 247 SymbolTable = i; 248 ++i; 249 FirstRegular = i; 250 ec = object_error::success; 251 return; 252 } 253 254 if (Name.startswith("#1/")) { 255 Format = K_BSD; 256 // We know this is BSD, so getName will work since there is no string table. 257 ec = i->getName(Name); 258 if (ec) 259 return; 260 if (Name == "__.SYMDEF SORTED") { 261 SymbolTable = i; 262 ++i; 263 } 264 FirstRegular = i; 265 return; 266 } 267 268 if (Name == "/") { 269 SymbolTable = i; 270 271 ++i; 272 if (i == e) { 273 ec = object_error::parse_failed; 274 return; 275 } 276 Name = i->getRawName(); 277 } 278 279 if (Name == "//") { 280 Format = K_GNU; 281 StringTable = i; 282 ++i; 283 FirstRegular = i; 284 ec = object_error::success; 285 return; 286 } 287 288 if (Name[0] != '/') { 289 Format = K_GNU; 290 FirstRegular = i; 291 ec = object_error::success; 292 return; 293 } 294 295 if (Name != "/") { 296 ec = object_error::parse_failed; 297 return; 298 } 299 300 Format = K_COFF; 301 SymbolTable = i; 302 303 ++i; 304 if (i == e) { 305 FirstRegular = i; 306 ec = object_error::success; 307 return; 308 } 309 310 Name = i->getRawName(); 311 312 if (Name == "//") { 313 StringTable = i; 314 ++i; 315 } 316 317 FirstRegular = i; 318 ec = object_error::success; 319 } 320 321 Archive::child_iterator Archive::child_begin(bool SkipInternal) const { 322 if (Data->getBufferSize() == 8) // empty archive. 323 return child_end(); 324 325 if (SkipInternal) 326 return FirstRegular; 327 328 const char *Loc = Data->getBufferStart() + strlen(Magic); 329 Child c(this, Loc); 330 return c; 331 } 332 333 Archive::child_iterator Archive::child_end() const { 334 return Child(this, NULL); 335 } 336 337 error_code Archive::Symbol::getName(StringRef &Result) const { 338 Result = StringRef(Parent->SymbolTable->getBuffer().begin() + StringIndex); 339 return object_error::success; 340 } 341 342 error_code Archive::Symbol::getMember(child_iterator &Result) const { 343 const char *Buf = Parent->SymbolTable->getBuffer().begin(); 344 const char *Offsets = Buf + 4; 345 uint32_t Offset = 0; 346 if (Parent->kind() == K_GNU) { 347 Offset = *(reinterpret_cast<const support::ubig32_t*>(Offsets) 348 + SymbolIndex); 349 } else if (Parent->kind() == K_BSD) { 350 llvm_unreachable("BSD format is not supported"); 351 } else { 352 uint32_t MemberCount = *reinterpret_cast<const support::ulittle32_t*>(Buf); 353 354 // Skip offsets. 355 Buf += sizeof(support::ulittle32_t) 356 + (MemberCount * sizeof(support::ulittle32_t)); 357 358 uint32_t SymbolCount = *reinterpret_cast<const support::ulittle32_t*>(Buf); 359 360 if (SymbolIndex >= SymbolCount) 361 return object_error::parse_failed; 362 363 // Skip SymbolCount to get to the indices table. 364 const char *Indices = Buf + sizeof(support::ulittle32_t); 365 366 // Get the index of the offset in the file member offset table for this 367 // symbol. 368 uint16_t OffsetIndex = 369 *(reinterpret_cast<const support::ulittle16_t*>(Indices) 370 + SymbolIndex); 371 // Subtract 1 since OffsetIndex is 1 based. 372 --OffsetIndex; 373 374 if (OffsetIndex >= MemberCount) 375 return object_error::parse_failed; 376 377 Offset = *(reinterpret_cast<const support::ulittle32_t*>(Offsets) 378 + OffsetIndex); 379 } 380 381 const char *Loc = Parent->getData().begin() + Offset; 382 Result = Child(Parent, Loc); 383 384 return object_error::success; 385 } 386 387 Archive::Symbol Archive::Symbol::getNext() const { 388 Symbol t(*this); 389 // Go to one past next null. 390 t.StringIndex = 391 Parent->SymbolTable->getBuffer().find('\0', t.StringIndex) + 1; 392 ++t.SymbolIndex; 393 return t; 394 } 395 396 Archive::symbol_iterator Archive::symbol_begin() const { 397 if (!hasSymbolTable()) 398 return symbol_iterator(Symbol(this, 0, 0)); 399 400 const char *buf = SymbolTable->getBuffer().begin(); 401 if (kind() == K_GNU) { 402 uint32_t symbol_count = 0; 403 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf); 404 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t))); 405 } else if (kind() == K_BSD) { 406 llvm_unreachable("BSD archive format is not supported"); 407 } else { 408 uint32_t member_count = 0; 409 uint32_t symbol_count = 0; 410 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf); 411 buf += 4 + (member_count * 4); // Skip offsets. 412 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf); 413 buf += 4 + (symbol_count * 2); // Skip indices. 414 } 415 uint32_t string_start_offset = buf - SymbolTable->getBuffer().begin(); 416 return symbol_iterator(Symbol(this, 0, string_start_offset)); 417 } 418 419 Archive::symbol_iterator Archive::symbol_end() const { 420 if (!hasSymbolTable()) 421 return symbol_iterator(Symbol(this, 0, 0)); 422 423 const char *buf = SymbolTable->getBuffer().begin(); 424 uint32_t symbol_count = 0; 425 if (kind() == K_GNU) { 426 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf); 427 } else if (kind() == K_BSD) { 428 llvm_unreachable("BSD archive format is not supported"); 429 } else { 430 uint32_t member_count = 0; 431 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf); 432 buf += 4 + (member_count * 4); // Skip offsets. 433 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf); 434 } 435 return symbol_iterator( 436 Symbol(this, symbol_count, 0)); 437 } 438 439 Archive::child_iterator Archive::findSym(StringRef name) const { 440 Archive::symbol_iterator bs = symbol_begin(); 441 Archive::symbol_iterator es = symbol_end(); 442 Archive::child_iterator result; 443 444 StringRef symname; 445 for (; bs != es; ++bs) { 446 if (bs->getName(symname)) 447 return child_end(); 448 if (symname == name) { 449 if (bs->getMember(result)) 450 return child_end(); 451 return result; 452 } 453 } 454 return child_end(); 455 } 456 457 bool Archive::hasSymbolTable() const { 458 return SymbolTable != child_end(); 459 } 460