| 1 | //===- GOFFObjectFile.cpp - GOFF object file implementation -----*- C++ -*-===// |
| 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 | // Implementation of the GOFFObjectFile class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/Object/GOFFObjectFile.h" |
| 14 | #include "llvm/BinaryFormat/GOFF.h" |
| 15 | #include "llvm/Object/GOFF.h" |
| 16 | #include "llvm/Support/DataExtractor.h" |
| 17 | #include "llvm/Support/Debug.h" |
| 18 | #include "llvm/Support/Errc.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | |
| 21 | #ifndef DEBUG_TYPE |
| 22 | #define DEBUG_TYPE "goff" |
| 23 | #endif |
| 24 | |
| 25 | using namespace llvm::object; |
| 26 | using namespace llvm; |
| 27 | |
| 28 | // Return the type of the record. |
| 29 | static GOFF::RecordType getRecordType(const uint8_t *PhysicalRecord) { |
| 30 | return GOFF::RecordType((PhysicalRecord[1] & 0xF0) >> 4); |
| 31 | } |
| 32 | |
| 33 | // Return true if the record is a continuation record. |
| 34 | static bool isContinuation(const uint8_t *PhysicalRecord) { |
| 35 | return PhysicalRecord[1] & 0x02; |
| 36 | } |
| 37 | |
| 38 | // Return true if the record has a continuation. |
| 39 | static bool isContinued(const uint8_t *PhysicalRecord) { |
| 40 | return PhysicalRecord[1] & 0x01; |
| 41 | } |
| 42 | |
| 43 | // Helper function to get continuous data from a logical record |
| 44 | // Includes PTV header + everything from first record + continuation payloads |
| 45 | // Returns the number of physical records consumed (including the initial |
| 46 | // record) |
| 47 | Expected<unsigned> |
| 48 | GOFFObjectFile::getContinuousData(SmallVectorImpl<uint8_t> &CompleteData, |
| 49 | int DataIndex, uint16_t DataLength, |
| 50 | const uint8_t *Record) const { |
| 51 | |
| 52 | CompleteData.reserve(N: DataLength + GOFF::RecordLength - DataIndex); |
| 53 | |
| 54 | // First record - include PTV header (bytes 0-2) |
| 55 | CompleteData.append(in_start: Record, in_end: Record + GOFF::RecordPrefixLength); |
| 56 | // Append everything from the first record before the start of the data. |
| 57 | CompleteData.append(in_start: Record + GOFF::RecordPrefixLength, in_end: Record + DataIndex); |
| 58 | // Append the data. |
| 59 | const uint8_t *Ptr = Record + DataIndex; |
| 60 | size_t SliceLength = std::min( |
| 61 | a: DataLength, b: static_cast<uint16_t>(GOFF::RecordLength - DataIndex)); |
| 62 | CompleteData.append(in_start: Ptr, in_end: Ptr + SliceLength); |
| 63 | DataLength -= SliceLength; |
| 64 | Ptr += SliceLength; |
| 65 | |
| 66 | unsigned BlocksConsumed = 1; // Count the initial record |
| 67 | // Continuation records. |
| 68 | while (DataLength > 0) { |
| 69 | // Ptr now points to the start of the next physical record. |
| 70 | // Check that this block is a Continuation. |
| 71 | assert(isContinuation(Ptr) && "Continuation bit must be set" ); |
| 72 | // Check that the last Continuation is terminated correctly. |
| 73 | if (DataLength <= GOFF::PayloadLength && isContinued(PhysicalRecord: Ptr)) |
| 74 | return createStringError(EC: object_error::parse_failed, |
| 75 | S: "continued bit should not be set" ); |
| 76 | |
| 77 | SliceLength = |
| 78 | std::min(a: DataLength, b: static_cast<uint16_t>(GOFF::PayloadLength)); |
| 79 | Ptr += GOFF::RecordPrefixLength; // Skip the 3-byte prefix |
| 80 | CompleteData.append(in_start: Ptr, in_end: Ptr + SliceLength); |
| 81 | DataLength -= SliceLength; |
| 82 | // Advance to the start of the next record |
| 83 | Ptr += (GOFF::RecordLength - GOFF::RecordPrefixLength); |
| 84 | BlocksConsumed++; |
| 85 | } |
| 86 | return BlocksConsumed; |
| 87 | } |
| 88 | |
| 89 | // Walk over the object file and populate FlattenedData. |
| 90 | Error GOFFObjectFile::createFlattenedData() { |
| 91 | const uint8_t *It = base(); |
| 92 | const uint8_t *End = base() + getData().size(); |
| 93 | |
| 94 | // First pass: validate continuation records. |
| 95 | const uint8_t *ValidateIt = It; |
| 96 | unsigned ValidateIndex = 0; |
| 97 | bool PrevContinued = false; |
| 98 | bool PrevWasContinuation = false; |
| 99 | GOFF::RecordType PrevRecordType = GOFF::RT_HDR; |
| 100 | |
| 101 | while (ValidateIt < End) { |
| 102 | bool IsCont = isContinuation(PhysicalRecord: ValidateIt); |
| 103 | bool IsContd = isContinued(PhysicalRecord: ValidateIt); |
| 104 | GOFF::RecordType CurrentType = ::getRecordType(PhysicalRecord: ValidateIt); |
| 105 | |
| 106 | if (IsCont) { |
| 107 | // Continuation record must be preceded by a continued record. |
| 108 | if (!PrevContinued) { |
| 109 | return createStringError(EC: object_error::parse_failed, |
| 110 | S: "record " + std::to_string(val: ValidateIndex) + |
| 111 | " is a continuation record that is not " |
| 112 | "preceded by a continued record" ); |
| 113 | } |
| 114 | // Continuation record type must match previous record type. |
| 115 | if (CurrentType != PrevRecordType) { |
| 116 | return createStringError( |
| 117 | EC: object_error::parse_failed, |
| 118 | S: "record " + std::to_string(val: ValidateIndex) + |
| 119 | " is a continuation record that does not match " |
| 120 | "the type of the previous record" ); |
| 121 | } |
| 122 | // Update PrevContinued for continuation records. |
| 123 | PrevContinued = IsContd; |
| 124 | } else { |
| 125 | // Check if previous non-continuation was marked as continued. |
| 126 | if (PrevContinued && !PrevWasContinuation) { |
| 127 | return createStringError(EC: object_error::parse_failed, |
| 128 | S: "record " + std::to_string(val: ValidateIndex) + |
| 129 | " is not a continuation record but the " |
| 130 | "preceding record is continued" ); |
| 131 | } |
| 132 | PrevRecordType = CurrentType; |
| 133 | PrevContinued = IsContd; |
| 134 | } |
| 135 | |
| 136 | PrevWasContinuation = IsCont; |
| 137 | ValidateIt += GOFF::RecordLength; |
| 138 | ValidateIndex++; |
| 139 | } |
| 140 | |
| 141 | // Second pass: process records now that we know they're valid. |
| 142 | while (It < End) { |
| 143 | // Skip continuation records - only process first physical record of each |
| 144 | // logical record. |
| 145 | if (isContinuation(PhysicalRecord: It)) { |
| 146 | It += GOFF::RecordLength; |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | GOFF::RecordType RecordType = ::getRecordType(PhysicalRecord: It); |
| 151 | |
| 152 | // Call get continuous data based on record type. |
| 153 | int DataIndex = 0; |
| 154 | uint16_t DataLength = 0; |
| 155 | ArrayRef<uint8_t> Slice(It, GOFF::RecordLength); |
| 156 | DataExtractor DE(Slice, false); |
| 157 | |
| 158 | switch (RecordType) { |
| 159 | case GOFF::RT_ESD: { |
| 160 | DataIndex = 72; |
| 161 | uint64_t Offset = 70; |
| 162 | DataLength = DE.getU16(offset_ptr: &Offset); |
| 163 | break; |
| 164 | } |
| 165 | case GOFF::RT_TXT: { |
| 166 | DataIndex = 24; |
| 167 | uint64_t Offset = 22; |
| 168 | DataLength = DE.getU16(offset_ptr: &Offset); |
| 169 | break; |
| 170 | } |
| 171 | case GOFF::RT_RLD: { |
| 172 | DataIndex = 6; |
| 173 | uint64_t Offset = 4; |
| 174 | DataLength = DE.getU16(offset_ptr: &Offset); |
| 175 | break; |
| 176 | } |
| 177 | case GOFF::RT_LEN: { |
| 178 | DataIndex = 8; |
| 179 | uint64_t Offset = 6; |
| 180 | DataLength = DE.getU16(offset_ptr: &Offset); |
| 181 | break; |
| 182 | } |
| 183 | case GOFF::RT_END: { |
| 184 | DataIndex = 26; |
| 185 | uint64_t Offset = 24; |
| 186 | DataLength = DE.getU16(offset_ptr: &Offset); |
| 187 | break; |
| 188 | } |
| 189 | case GOFF::RT_HDR: { |
| 190 | DataIndex = 60; |
| 191 | uint64_t Offset = 52; |
| 192 | DataLength = DE.getU16(offset_ptr: &Offset); |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | // Get the flattened data for this logical record (including continuations). |
| 197 | SmallVector<uint8_t> CompleteData; |
| 198 | Expected<unsigned> BlocksConsumed = |
| 199 | getContinuousData(CompleteData, DataIndex, DataLength, Record: It); |
| 200 | if (!BlocksConsumed) { |
| 201 | // Log the error but don't fail construction - errors in continuation |
| 202 | // data will be caught when the data is actually accessed. |
| 203 | llvm::handleAllErrors( |
| 204 | E: BlocksConsumed.takeError(), Handlers: [](const llvm::ErrorInfoBase &EIB) { |
| 205 | llvm::errs() << "ERROR: " << EIB.message() << "\n" ; |
| 206 | }); |
| 207 | // Skip this record and continue. |
| 208 | It += GOFF::RecordLength; |
| 209 | continue; |
| 210 | } |
| 211 | FlattenedData.push_back(Elt: {RecordType, std::move(CompleteData)}); |
| 212 | |
| 213 | // Move to next logical record using the number of blocks consumed. |
| 214 | It += (*BlocksConsumed) * GOFF::RecordLength; |
| 215 | } |
| 216 | return Error::success(); |
| 217 | } |
| 218 | |
| 219 | Expected<std::unique_ptr<ObjectFile>> |
| 220 | ObjectFile::createGOFFObjectFile(MemoryBufferRef Object) { |
| 221 | Error Err = Error::success(); |
| 222 | std::unique_ptr<GOFFObjectFile> Ret(new GOFFObjectFile(Object, Err)); |
| 223 | if (Err) |
| 224 | return std::move(Err); |
| 225 | return std::move(Ret); |
| 226 | } |
| 227 | |
| 228 | GOFFObjectFile::GOFFObjectFile(MemoryBufferRef Object, Error &Err) |
| 229 | : ObjectFile(Binary::ID_GOFF, Object) { |
| 230 | ErrorAsOutParameter ErrAsOutParam(Err); |
| 231 | // Object file isn't the right size, bail out early. |
| 232 | if ((Object.getBufferSize() % GOFF::RecordLength) != 0) { |
| 233 | Err = createStringError( |
| 234 | EC: object_error::unexpected_eof, |
| 235 | S: "object file is not the right size. Must be a multiple " |
| 236 | "of 80 bytes, but is " + |
| 237 | std::to_string(val: Object.getBufferSize()) + " bytes" ); |
| 238 | return; |
| 239 | } |
| 240 | // Object file doesn't start/end with HDR/END records. |
| 241 | // Bail out early. |
| 242 | if (Object.getBufferSize() != 0) { |
| 243 | if ((base()[1] & 0xF0) >> 4 != GOFF::RT_HDR) { |
| 244 | Err = createStringError(EC: object_error::parse_failed, |
| 245 | S: "object file must start with HDR record" ); |
| 246 | return; |
| 247 | } |
| 248 | if ((base()[Object.getBufferSize() - GOFF::RecordLength + 1] & 0xF0) >> 4 != |
| 249 | GOFF::RT_END) { |
| 250 | Err = createStringError(EC: object_error::parse_failed, |
| 251 | S: "object file must end with END record" ); |
| 252 | return; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if (Error E = createFlattenedData()) { |
| 257 | Err = std::move(E); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | SectionEntryImpl DummySection; |
| 262 | SectionList.emplace_back(Args&: DummySection); // Dummy entry at index 0. |
| 263 | |
| 264 | for (const auto &[RecordType, Data] : FlattenedData) { |
| 265 | const uint8_t *I = Data.data(); |
| 266 | switch (RecordType) { |
| 267 | case GOFF::RT_ESD: { |
| 268 | // Save ESD record. |
| 269 | uint32_t EsdId; |
| 270 | ESDRecord::getEsdId(Record: I, EsdId); |
| 271 | EsdPtrs.grow(N: EsdId); |
| 272 | EsdPtrs[EsdId] = I; |
| 273 | |
| 274 | // Determine and save the "sections" in GOFF. |
| 275 | // A section is saved as a tuple of the form |
| 276 | // case (1): (ED,child PR) |
| 277 | // - where the PR must have non-zero length. |
| 278 | // case (2a) (ED,0) |
| 279 | // - where the ED is of non-zero length. |
| 280 | // case (2b) (ED,0) |
| 281 | // - where the ED is zero length but |
| 282 | // contains a label (LD). |
| 283 | GOFF::ESDSymbolType SymbolType; |
| 284 | ESDRecord::getSymbolType(Record: I, SymbolType); |
| 285 | SectionEntryImpl Section; |
| 286 | uint32_t Length; |
| 287 | ESDRecord::getLength(Record: I, Length); |
| 288 | if (SymbolType == GOFF::ESD_ST_ElementDefinition) { |
| 289 | // case (2a) |
| 290 | if (Length != 0) { |
| 291 | Section.d.a = EsdId; |
| 292 | SectionList.emplace_back(Args&: Section); |
| 293 | } |
| 294 | } else if (SymbolType == GOFF::ESD_ST_PartReference) { |
| 295 | // case (1) |
| 296 | if (Length != 0) { |
| 297 | uint32_t SymEdId; |
| 298 | ESDRecord::getParentEsdId(Record: I, EsdId&: SymEdId); |
| 299 | Section.d.a = SymEdId; |
| 300 | Section.d.b = EsdId; |
| 301 | SectionList.emplace_back(Args&: Section); |
| 302 | } |
| 303 | } else if (SymbolType == GOFF::ESD_ST_LabelDefinition) { |
| 304 | // case (2b) |
| 305 | uint32_t SymEdId; |
| 306 | ESDRecord::getParentEsdId(Record: I, EsdId&: SymEdId); |
| 307 | const uint8_t *SymEdRecord = EsdPtrs[SymEdId]; |
| 308 | uint32_t EdLength; |
| 309 | ESDRecord::getLength(Record: SymEdRecord, Length&: EdLength); |
| 310 | if (!EdLength) { // [ EDID, PRID ] |
| 311 | // LD child of a zero length parent ED. |
| 312 | // Add the section ED which was previously ignored. |
| 313 | Section.d.a = SymEdId; |
| 314 | SectionList.emplace_back(Args&: Section); |
| 315 | } |
| 316 | } |
| 317 | LLVM_DEBUG(dbgs() << " -- ESD " << EsdId << "\n" ); |
| 318 | break; |
| 319 | } |
| 320 | case GOFF::RT_TXT: |
| 321 | // Save TXT records. |
| 322 | TextPtrs.emplace_back(Args&: I); |
| 323 | LLVM_DEBUG(dbgs() << " -- TXT\n" ); |
| 324 | break; |
| 325 | case GOFF::RT_RLD: |
| 326 | LLVM_DEBUG(dbgs() << " -- RLD (GOFF record type) unhandled\n" ); |
| 327 | break; |
| 328 | case GOFF::RT_LEN: |
| 329 | LLVM_DEBUG(dbgs() << " -- LEN (GOFF record type) unhandled\n" ); |
| 330 | break; |
| 331 | case GOFF::RT_END: |
| 332 | LLVM_DEBUG(dbgs() << " -- END (GOFF record type) unhandled\n" ); |
| 333 | break; |
| 334 | case GOFF::RT_HDR: |
| 335 | LLVM_DEBUG(dbgs() << " -- HDR (GOFF record type) unhandled\n" ); |
| 336 | break; |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | const uint8_t *GOFFObjectFile::getSymbolEsdRecord(DataRefImpl Symb) const { |
| 342 | const uint8_t *EsdRecord = EsdPtrs[Symb.d.a]; |
| 343 | return EsdRecord; |
| 344 | } |
| 345 | |
| 346 | Expected<StringRef> GOFFObjectFile::getSymbolName(DataRefImpl Symb) const { |
| 347 | if (auto It = EsdNamesCache.find(Val: Symb.d.a); It != EsdNamesCache.end()) { |
| 348 | auto &StrPtr = It->second; |
| 349 | return StringRef(StrPtr.second.get(), StrPtr.first); |
| 350 | } |
| 351 | |
| 352 | // Get the ESD record pointer from EsdPtrs (points to FlattenedData) |
| 353 | const uint8_t *EsdRecord = getSymbolEsdRecord(Symb); |
| 354 | // Extract name from the flattened ESD record |
| 355 | // Name length is at byte 70-71, name data starts at byte 72 |
| 356 | uint16_t NameLength = ESDRecord::getNameLength(Record: EsdRecord); |
| 357 | SmallString<256> SymbolName; |
| 358 | if (NameLength > 0) { |
| 359 | // Name starts at byte 72 in the record (already flattened, no |
| 360 | // continuations) |
| 361 | const uint8_t *NameStart = EsdRecord + 72; |
| 362 | SymbolName.append(in_start: NameStart, in_end: NameStart + NameLength); |
| 363 | } |
| 364 | |
| 365 | SmallString<256> SymbolNameConverted; |
| 366 | ConverterEBCDIC::convertToUTF8(Source: SymbolName, Result&: SymbolNameConverted); |
| 367 | |
| 368 | size_t Size = SymbolNameConverted.size(); |
| 369 | auto StrPtr = std::make_pair(x&: Size, y: std::make_unique<char[]>(num: Size)); |
| 370 | char *Buf = StrPtr.second.get(); |
| 371 | memcpy(dest: Buf, src: SymbolNameConverted.data(), n: Size); |
| 372 | EsdNamesCache[Symb.d.a] = std::move(StrPtr); |
| 373 | return StringRef(Buf, Size); |
| 374 | } |
| 375 | |
| 376 | Expected<StringRef> GOFFObjectFile::getSymbolName(SymbolRef Symbol) const { |
| 377 | return getSymbolName(Symb: Symbol.getRawDataRefImpl()); |
| 378 | } |
| 379 | |
| 380 | Expected<uint64_t> GOFFObjectFile::getSymbolAddress(DataRefImpl Symb) const { |
| 381 | uint32_t Offset; |
| 382 | const uint8_t *EsdRecord = getSymbolEsdRecord(Symb); |
| 383 | ESDRecord::getOffset(Record: EsdRecord, Offset); |
| 384 | return static_cast<uint64_t>(Offset); |
| 385 | } |
| 386 | |
| 387 | uint64_t GOFFObjectFile::getSymbolValueImpl(DataRefImpl Symb) const { |
| 388 | uint32_t Offset; |
| 389 | const uint8_t *EsdRecord = getSymbolEsdRecord(Symb); |
| 390 | ESDRecord::getOffset(Record: EsdRecord, Offset); |
| 391 | return static_cast<uint64_t>(Offset); |
| 392 | } |
| 393 | |
| 394 | uint64_t GOFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const { |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | bool GOFFObjectFile::isSymbolUnresolved(DataRefImpl Symb) const { |
| 399 | const uint8_t *Record = getSymbolEsdRecord(Symb); |
| 400 | GOFF::ESDSymbolType SymbolType; |
| 401 | ESDRecord::getSymbolType(Record, SymbolType); |
| 402 | |
| 403 | if (SymbolType == GOFF::ESD_ST_ExternalReference) |
| 404 | return true; |
| 405 | if (SymbolType == GOFF::ESD_ST_PartReference) { |
| 406 | uint32_t Length; |
| 407 | ESDRecord::getLength(Record, Length); |
| 408 | if (Length == 0) |
| 409 | return true; |
| 410 | } |
| 411 | return false; |
| 412 | } |
| 413 | |
| 414 | bool GOFFObjectFile::isSymbolIndirect(DataRefImpl Symb) const { |
| 415 | const uint8_t *Record = getSymbolEsdRecord(Symb); |
| 416 | bool Indirect; |
| 417 | ESDRecord::getIndirectReference(Record, Indirect); |
| 418 | return Indirect; |
| 419 | } |
| 420 | |
| 421 | Expected<uint32_t> GOFFObjectFile::getSymbolFlags(DataRefImpl Symb) const { |
| 422 | uint32_t Flags = 0; |
| 423 | if (isSymbolUnresolved(Symb)) |
| 424 | Flags |= SymbolRef::SF_Undefined; |
| 425 | |
| 426 | const uint8_t *Record = getSymbolEsdRecord(Symb); |
| 427 | |
| 428 | GOFF::ESDBindingStrength BindingStrength; |
| 429 | ESDRecord::getBindingStrength(Record, Strength&: BindingStrength); |
| 430 | if (BindingStrength == GOFF::ESD_BST_Weak) |
| 431 | Flags |= SymbolRef::SF_Weak; |
| 432 | |
| 433 | GOFF::ESDBindingScope BindingScope; |
| 434 | ESDRecord::getBindingScope(Record, Scope&: BindingScope); |
| 435 | |
| 436 | GOFF::ESDSymbolType Type; |
| 437 | ESDRecord::getSymbolType(Record, SymbolType&: Type); |
| 438 | |
| 439 | if (Type != GOFF::ESD_ST_SectionDefinition && |
| 440 | Type != GOFF::ESD_ST_ElementDefinition && |
| 441 | BindingScope != GOFF::ESD_BSC_Section && |
| 442 | BindingScope != GOFF::ESD_BSC_Module) { |
| 443 | Expected<StringRef> Name = getSymbolName(Symb); |
| 444 | if (Name && *Name != " " ) { // Blank name is local. |
| 445 | Flags |= SymbolRef::SF_Global; |
| 446 | if (BindingScope == GOFF::ESD_BSC_ImportExport) |
| 447 | Flags |= SymbolRef::SF_Exported; |
| 448 | else if (!(Flags & SymbolRef::SF_Undefined)) |
| 449 | Flags |= SymbolRef::SF_Hidden; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | return Flags; |
| 454 | } |
| 455 | |
| 456 | Expected<SymbolRef::Type> |
| 457 | GOFFObjectFile::getSymbolType(DataRefImpl Symb) const { |
| 458 | const uint8_t *Record = getSymbolEsdRecord(Symb); |
| 459 | GOFF::ESDSymbolType SymbolType; |
| 460 | ESDRecord::getSymbolType(Record, SymbolType); |
| 461 | GOFF::ESDExecutable Executable; |
| 462 | ESDRecord::getExecutable(Record, Executable); |
| 463 | |
| 464 | if (SymbolType != GOFF::ESD_ST_SectionDefinition && |
| 465 | SymbolType != GOFF::ESD_ST_ElementDefinition && |
| 466 | SymbolType != GOFF::ESD_ST_LabelDefinition && |
| 467 | SymbolType != GOFF::ESD_ST_PartReference && |
| 468 | SymbolType != GOFF::ESD_ST_ExternalReference) { |
| 469 | uint32_t EsdId; |
| 470 | ESDRecord::getEsdId(Record, EsdId); |
| 471 | return createStringError(EC: llvm::errc::invalid_argument, |
| 472 | Fmt: "ESD record %" PRIu32 |
| 473 | " has invalid symbol type 0x%02" PRIX8, |
| 474 | Vals: EsdId, Vals: SymbolType); |
| 475 | } |
| 476 | switch (SymbolType) { |
| 477 | case GOFF::ESD_ST_SectionDefinition: |
| 478 | case GOFF::ESD_ST_ElementDefinition: |
| 479 | return SymbolRef::ST_Other; |
| 480 | case GOFF::ESD_ST_LabelDefinition: |
| 481 | case GOFF::ESD_ST_PartReference: |
| 482 | case GOFF::ESD_ST_ExternalReference: |
| 483 | if (Executable != GOFF::ESD_EXE_CODE && Executable != GOFF::ESD_EXE_DATA && |
| 484 | Executable != GOFF::ESD_EXE_Unspecified) { |
| 485 | uint32_t EsdId; |
| 486 | ESDRecord::getEsdId(Record, EsdId); |
| 487 | return createStringError(EC: llvm::errc::invalid_argument, |
| 488 | Fmt: "ESD record %" PRIu32 |
| 489 | " has unknown Executable type 0x%02X" , |
| 490 | Vals: EsdId, Vals: Executable); |
| 491 | } |
| 492 | switch (Executable) { |
| 493 | case GOFF::ESD_EXE_CODE: |
| 494 | return SymbolRef::ST_Function; |
| 495 | case GOFF::ESD_EXE_DATA: |
| 496 | return SymbolRef::ST_Data; |
| 497 | case GOFF::ESD_EXE_Unspecified: |
| 498 | return SymbolRef::ST_Unknown; |
| 499 | } |
| 500 | llvm_unreachable("Unhandled ESDExecutable" ); |
| 501 | } |
| 502 | llvm_unreachable("Unhandled ESDSymbolType" ); |
| 503 | } |
| 504 | |
| 505 | Expected<section_iterator> |
| 506 | GOFFObjectFile::getSymbolSection(DataRefImpl Symb) const { |
| 507 | DataRefImpl Sec; |
| 508 | |
| 509 | if (isSymbolUnresolved(Symb)) |
| 510 | return section_iterator(SectionRef(Sec, this)); |
| 511 | |
| 512 | const uint8_t *SymEsdRecord = EsdPtrs[Symb.d.a]; |
| 513 | uint32_t SymEdId; |
| 514 | ESDRecord::getParentEsdId(Record: SymEsdRecord, EsdId&: SymEdId); |
| 515 | const uint8_t *SymEdRecord = EsdPtrs[SymEdId]; |
| 516 | |
| 517 | for (size_t I = 0, E = SectionList.size(); I < E; ++I) { |
| 518 | bool Found; |
| 519 | const uint8_t *SectionPrRecord = getSectionPrEsdRecord(SectionIndex: I); |
| 520 | if (SectionPrRecord) { |
| 521 | Found = SymEsdRecord == SectionPrRecord; |
| 522 | } else { |
| 523 | const uint8_t *SectionEdRecord = getSectionEdEsdRecord(SectionIndex: I); |
| 524 | Found = SymEdRecord == SectionEdRecord; |
| 525 | } |
| 526 | |
| 527 | if (Found) { |
| 528 | Sec.d.a = I; |
| 529 | return section_iterator(SectionRef(Sec, this)); |
| 530 | } |
| 531 | } |
| 532 | return createStringError(EC: llvm::errc::invalid_argument, |
| 533 | S: "symbol with ESD id " + std::to_string(val: Symb.d.a) + |
| 534 | " refers to invalid section with ESD id " + |
| 535 | std::to_string(val: SymEdId)); |
| 536 | } |
| 537 | |
| 538 | uint64_t GOFFObjectFile::getSymbolSize(DataRefImpl Symb) const { |
| 539 | const uint8_t *Record = getSymbolEsdRecord(Symb); |
| 540 | uint32_t Length; |
| 541 | ESDRecord::getLength(Record, Length); |
| 542 | return Length; |
| 543 | } |
| 544 | |
| 545 | const uint8_t *GOFFObjectFile::getSectionEdEsdRecord(DataRefImpl &Sec) const { |
| 546 | SectionEntryImpl EsdIds = SectionList[Sec.d.a]; |
| 547 | const uint8_t *EsdRecord = EsdPtrs[EsdIds.d.a]; |
| 548 | return EsdRecord; |
| 549 | } |
| 550 | |
| 551 | const uint8_t *GOFFObjectFile::getSectionPrEsdRecord(DataRefImpl &Sec) const { |
| 552 | SectionEntryImpl EsdIds = SectionList[Sec.d.a]; |
| 553 | const uint8_t *EsdRecord = nullptr; |
| 554 | if (EsdIds.d.b) |
| 555 | EsdRecord = EsdPtrs[EsdIds.d.b]; |
| 556 | return EsdRecord; |
| 557 | } |
| 558 | |
| 559 | const uint8_t * |
| 560 | GOFFObjectFile::getSectionEdEsdRecord(uint32_t SectionIndex) const { |
| 561 | DataRefImpl Sec; |
| 562 | Sec.d.a = SectionIndex; |
| 563 | const uint8_t *EsdRecord = getSectionEdEsdRecord(Sec); |
| 564 | return EsdRecord; |
| 565 | } |
| 566 | |
| 567 | const uint8_t * |
| 568 | GOFFObjectFile::getSectionPrEsdRecord(uint32_t SectionIndex) const { |
| 569 | DataRefImpl Sec; |
| 570 | Sec.d.a = SectionIndex; |
| 571 | const uint8_t *EsdRecord = getSectionPrEsdRecord(Sec); |
| 572 | return EsdRecord; |
| 573 | } |
| 574 | |
| 575 | uint32_t GOFFObjectFile::getSectionDefEsdId(DataRefImpl &Sec) const { |
| 576 | const uint8_t *EsdRecord = getSectionEdEsdRecord(Sec); |
| 577 | uint32_t Length; |
| 578 | ESDRecord::getLength(Record: EsdRecord, Length); |
| 579 | if (Length == 0) { |
| 580 | const uint8_t *PrEsdRecord = getSectionPrEsdRecord(Sec); |
| 581 | if (PrEsdRecord) |
| 582 | EsdRecord = PrEsdRecord; |
| 583 | } |
| 584 | |
| 585 | uint32_t DefEsdId; |
| 586 | ESDRecord::getEsdId(Record: EsdRecord, EsdId&: DefEsdId); |
| 587 | LLVM_DEBUG(dbgs() << "Got def EsdId: " << DefEsdId << '\n'); |
| 588 | return DefEsdId; |
| 589 | } |
| 590 | |
| 591 | void GOFFObjectFile::moveSectionNext(DataRefImpl &Sec) const { |
| 592 | Sec.d.a++; |
| 593 | if ((Sec.d.a) >= SectionList.size()) |
| 594 | Sec.d.a = 0; |
| 595 | } |
| 596 | |
| 597 | Expected<StringRef> GOFFObjectFile::getSectionName(DataRefImpl Sec) const { |
| 598 | DataRefImpl EdSym; |
| 599 | SectionEntryImpl EsdIds = SectionList[Sec.d.a]; |
| 600 | EdSym.d.a = EsdIds.d.a; |
| 601 | Expected<StringRef> Name = getSymbolName(Symb: EdSym); |
| 602 | if (Name) { |
| 603 | StringRef Res = *Name; |
| 604 | LLVM_DEBUG(dbgs() << "Got section: " << Res << '\n'); |
| 605 | LLVM_DEBUG(dbgs() << "Final section name: " << Res << '\n'); |
| 606 | Name = Res; |
| 607 | } |
| 608 | return Name; |
| 609 | } |
| 610 | |
| 611 | uint64_t GOFFObjectFile::getSectionAddress(DataRefImpl Sec) const { |
| 612 | uint32_t Offset; |
| 613 | const uint8_t *EsdRecord = getSectionEdEsdRecord(Sec); |
| 614 | ESDRecord::getOffset(Record: EsdRecord, Offset); |
| 615 | return Offset; |
| 616 | } |
| 617 | |
| 618 | uint64_t GOFFObjectFile::getSectionSize(DataRefImpl Sec) const { |
| 619 | uint32_t Length; |
| 620 | uint32_t DefEsdId = getSectionDefEsdId(Sec); |
| 621 | const uint8_t *EsdRecord = EsdPtrs[DefEsdId]; |
| 622 | ESDRecord::getLength(Record: EsdRecord, Length); |
| 623 | LLVM_DEBUG(dbgs() << "Got section size: " << Length << '\n'); |
| 624 | return static_cast<uint64_t>(Length); |
| 625 | } |
| 626 | |
| 627 | // Unravel TXT records and expand fill characters to produce |
| 628 | // a contiguous sequence of bytes. |
| 629 | Expected<ArrayRef<uint8_t>> |
| 630 | GOFFObjectFile::getSectionContents(DataRefImpl Sec) const { |
| 631 | if (auto It = SectionDataCache.find(Val: Sec.d.a); It != SectionDataCache.end()) { |
| 632 | auto &Buf = It->second; |
| 633 | return ArrayRef<uint8_t>(Buf); |
| 634 | } |
| 635 | uint64_t SectionSize = getSectionSize(Sec); |
| 636 | uint32_t DefEsdId = getSectionDefEsdId(Sec); |
| 637 | |
| 638 | const uint8_t *EdEsdRecord = getSectionEdEsdRecord(Sec); |
| 639 | bool FillBytePresent; |
| 640 | ESDRecord::getFillBytePresent(Record: EdEsdRecord, Present&: FillBytePresent); |
| 641 | uint8_t FillByte = '\0'; |
| 642 | if (FillBytePresent) |
| 643 | ESDRecord::getFillByteValue(Record: EdEsdRecord, Fill&: FillByte); |
| 644 | |
| 645 | // Initialize section with fill byte. |
| 646 | SmallVector<uint8_t> Data(SectionSize, FillByte); |
| 647 | |
| 648 | // Replace section with content from text records. |
| 649 | for (const uint8_t *TxtRecordPtr : TextPtrs) { |
| 650 | uint32_t TxtEsdId; |
| 651 | TXTRecord::getElementEsdId(Record: TxtRecordPtr, EsdId&: TxtEsdId); |
| 652 | LLVM_DEBUG(dbgs() << "Got txt EsdId: " << TxtEsdId << '\n'); |
| 653 | |
| 654 | if (TxtEsdId != DefEsdId) |
| 655 | continue; |
| 656 | |
| 657 | uint32_t TxtDataOffset; |
| 658 | TXTRecord::getOffset(Record: TxtRecordPtr, Offset&: TxtDataOffset); |
| 659 | |
| 660 | uint16_t TxtDataSize; |
| 661 | TXTRecord::getDataLength(Record: TxtRecordPtr, Length&: TxtDataSize); |
| 662 | |
| 663 | LLVM_DEBUG(dbgs() << "Record offset " << TxtDataOffset << ", data size " |
| 664 | << TxtDataSize << "\n" ); |
| 665 | |
| 666 | // Text data starts at byte 24 in the flattened record (already processed |
| 667 | // continuations) |
| 668 | const uint8_t *TxtData = TxtRecordPtr + 24; |
| 669 | assert(TxtDataSize <= Data.size() - TxtDataOffset && |
| 670 | "Text data exceeds section size" ); |
| 671 | std::copy(first: TxtData, last: TxtData + TxtDataSize, result: Data.begin() + TxtDataOffset); |
| 672 | } |
| 673 | auto &Cache = SectionDataCache[Sec.d.a]; |
| 674 | Cache = std::move(Data); |
| 675 | return ArrayRef<uint8_t>(Cache); |
| 676 | } |
| 677 | |
| 678 | uint64_t GOFFObjectFile::getSectionAlignment(DataRefImpl Sec) const { |
| 679 | const uint8_t *EsdRecord = getSectionEdEsdRecord(Sec); |
| 680 | GOFF::ESDAlignment Pow2Alignment; |
| 681 | ESDRecord::getAlignment(Record: EsdRecord, Alignment&: Pow2Alignment); |
| 682 | return 1ULL << static_cast<uint64_t>(Pow2Alignment); |
| 683 | } |
| 684 | |
| 685 | bool GOFFObjectFile::isSectionText(DataRefImpl Sec) const { |
| 686 | const uint8_t *EsdRecord = getSectionEdEsdRecord(Sec); |
| 687 | GOFF::ESDExecutable Executable; |
| 688 | ESDRecord::getExecutable(Record: EsdRecord, Executable); |
| 689 | return Executable == GOFF::ESD_EXE_CODE; |
| 690 | } |
| 691 | |
| 692 | bool GOFFObjectFile::isSectionData(DataRefImpl Sec) const { |
| 693 | const uint8_t *EsdRecord = getSectionEdEsdRecord(Sec); |
| 694 | GOFF::ESDExecutable Executable; |
| 695 | ESDRecord::getExecutable(Record: EsdRecord, Executable); |
| 696 | return Executable == GOFF::ESD_EXE_DATA; |
| 697 | } |
| 698 | |
| 699 | bool GOFFObjectFile::isSectionNoLoad(DataRefImpl Sec) const { |
| 700 | const uint8_t *EsdRecord = getSectionEdEsdRecord(Sec); |
| 701 | GOFF::ESDLoadingBehavior LoadingBehavior; |
| 702 | ESDRecord::getLoadingBehavior(Record: EsdRecord, Behavior&: LoadingBehavior); |
| 703 | return LoadingBehavior == GOFF::ESD_LB_NoLoad; |
| 704 | } |
| 705 | |
| 706 | bool GOFFObjectFile::isSectionReadOnlyData(DataRefImpl Sec) const { |
| 707 | if (!isSectionData(Sec)) |
| 708 | return false; |
| 709 | |
| 710 | const uint8_t *EsdRecord = getSectionEdEsdRecord(Sec); |
| 711 | GOFF::ESDLoadingBehavior LoadingBehavior; |
| 712 | ESDRecord::getLoadingBehavior(Record: EsdRecord, Behavior&: LoadingBehavior); |
| 713 | return LoadingBehavior == GOFF::ESD_LB_Initial; |
| 714 | } |
| 715 | |
| 716 | bool GOFFObjectFile::isSectionZeroInit(DataRefImpl Sec) const { |
| 717 | // GOFF uses fill characters and fill characters are applied |
| 718 | // on getSectionContents() - so we say false to zero init. |
| 719 | return false; |
| 720 | } |
| 721 | |
| 722 | section_iterator GOFFObjectFile::section_begin() const { |
| 723 | DataRefImpl Sec; |
| 724 | moveSectionNext(Sec); |
| 725 | return section_iterator(SectionRef(Sec, this)); |
| 726 | } |
| 727 | |
| 728 | section_iterator GOFFObjectFile::section_end() const { |
| 729 | DataRefImpl Sec; |
| 730 | return section_iterator(SectionRef(Sec, this)); |
| 731 | } |
| 732 | |
| 733 | void GOFFObjectFile::moveSymbolNext(DataRefImpl &Symb) const { |
| 734 | for (uint32_t I = Symb.d.a + 1, E = EsdPtrs.size(); I < E; ++I) { |
| 735 | if (const uint8_t *EsdRecord = EsdPtrs[I]) { |
| 736 | GOFF::ESDSymbolType SymbolType; |
| 737 | ESDRecord::getSymbolType(Record: EsdRecord, SymbolType); |
| 738 | // Skip EDs - i.e. section symbols. |
| 739 | bool IgnoreSpecialGOFFSymbols = true; |
| 740 | bool SkipSymbol = ((SymbolType == GOFF::ESD_ST_ElementDefinition) || |
| 741 | (SymbolType == GOFF::ESD_ST_SectionDefinition)) && |
| 742 | IgnoreSpecialGOFFSymbols; |
| 743 | if (!SkipSymbol) { |
| 744 | Symb.d.a = I; |
| 745 | return; |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | Symb.d.a = 0; |
| 750 | } |
| 751 | |
| 752 | basic_symbol_iterator GOFFObjectFile::symbol_begin() const { |
| 753 | DataRefImpl Symb; |
| 754 | moveSymbolNext(Symb); |
| 755 | return basic_symbol_iterator(SymbolRef(Symb, this)); |
| 756 | } |
| 757 | |
| 758 | basic_symbol_iterator GOFFObjectFile::symbol_end() const { |
| 759 | DataRefImpl Symb; |
| 760 | return basic_symbol_iterator(SymbolRef(Symb, this)); |
| 761 | } |
| 762 | |