[llvm] [obj2yaml] Add ability to dump GOFF header records (PR #90871)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri May 3 00:21:27 PDT 2024


================
@@ -24,6 +25,94 @@
 using namespace llvm::object;
 using namespace llvm;
 
+namespace {
+// Return the type of the record.
+GOFF::RecordType getRecordType(const uint8_t *PhysicalRecord) {
+  return GOFF::RecordType((PhysicalRecord[1] & 0xF0) >> 4);
+}
+
+// Return true if the record is a continuation record.
+bool isContinuation(const uint8_t *PhysicalRecord) {
+  return PhysicalRecord[1] & 0x02;
+}
+
+// Return true if the record has a continuation.
+bool isContinued(const uint8_t *PhysicalRecord) {
+  return PhysicalRecord[1] & 0x01;
+}
+} // namespace
+
+void RecordRef::determineSize() {
+  GOFF::RecordType CurrRecordType = ::getRecordType(Data);
+  bool PrevWasContinued = isContinued(Data);
+  const uint8_t *It = Data + GOFF::RecordLength;
+  const uint8_t *End = reinterpret_cast<const uint8_t *>(
+      base() + OwningObject->getData().size());
+  for (; It < End;
+       PrevWasContinued = isContinued(It), It += GOFF::RecordLength) {
+    GOFF::RecordType RecordType = ::getRecordType(It);
+    bool IsContinuation = isContinuation(It);
+    size_t RecordNum = (It - base()) / GOFF::RecordLength;
+    // If the previous record was continued, the current record should be a
+    // continuation.
+    if (PrevWasContinued && !IsContinuation) {
+      createError(object_error::parse_failed,
+                  "record " + std::to_string(RecordNum) +
+                      " is not a continuation record but the "
+                      "preceding record is continued");
+      return;
+    }
+    // If the current record is a continuation, then the previous record should
+    // be continued, and have the same record type.
+    if (IsContinuation) {
+      if (RecordType != CurrRecordType) {
+        createError(object_error::parse_failed,
+                    "record " + std::to_string(RecordNum) +
----------------
MaskRay wrote:

`Twine(...)` is slight better since it avoids constructs many temporary std::string

https://github.com/llvm/llvm-project/pull/90871


More information about the llvm-commits mailing list