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

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon May 6 11:15:42 PDT 2024


================
@@ -24,6 +25,95 @@
 using namespace llvm::object;
 using namespace llvm;
 
+// Return the type of the record.
+static GOFF::RecordType getRecordType(const uint8_t *PhysicalRecord) {
+  return GOFF::RecordType((PhysicalRecord[1] & 0xF0) >> 4);
+}
+
+// Return true if the record is a continuation record.
+static bool isContinuation(const uint8_t *PhysicalRecord) {
+  return PhysicalRecord[1] & 0x02;
+}
+
+// Return true if the record has a continuation.
+static bool isContinued(const uint8_t *PhysicalRecord) {
+  return PhysicalRecord[1] & 0x01;
+}
+
+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,
+                  Twine("Record ")
+                      .concat(std::to_string(RecordNum))
+                      .concat(" 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,
+                    Twine("Record ")
+                        .concat(std::to_string(RecordNum))
----------------
MaskRay wrote:

`std::to_string` creates a temporary std::string. The overhead can be removed with `Twine(RecordNum)`

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


More information about the llvm-commits mailing list