[llvm] [llvm-symbolizer] Make symbolizer parse section relative syntax (PR #168524)

via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 24 22:54:47 PDT 2026


================
@@ -0,0 +1,157 @@
+## Test section-relative address syntax parsing and error handling for XCOFF.
+## This tests that the (SECTION_TYPE)(+offset) syntax produces appropriate
+## error messages for invalid inputs.
+
+## Create a simple XCOFF object for testing.
+# RUN: yaml2obj %s --docnum=1 -o %t.xcoff
+
+## Test invalid section type.
+# RUN: llvm-symbolizer --obj=%t.xcoff '(INVALID)(+0x10)' 2>&1 | \
+# RUN:   FileCheck %s -DTYPE=INVALID --check-prefix=INVALID-TYPE
+
+## Test unsupported section type (BSS is not supported).
+# RUN: llvm-symbolizer --obj=%t.xcoff '(BSS)(+0x10)' 2>&1 | \
+# RUN:   FileCheck %s -DTYPE=BSS --check-prefix=INVALID-TYPE
+
+## Test invalid section type containing a dash.
+# RUN: llvm-symbolizer --obj=%t.xcoff '(-)(+0x10)' 2>&1 | \
+# RUN:   FileCheck %s -DTYPE=- --check-prefix=INVALID-TYPE
+
+# INVALID-TYPE: error: '([[TYPE]])(+0x10)': unknown or unsupported section type "[[TYPE]]" in section-relative address
+
+## Test missing '+' sign.
+# RUN: llvm-symbolizer --obj=%t.xcoff '(TEXT)(0x10)' 2>&1 | \
+# RUN:   FileCheck %s --check-prefix=NO-PLUS
+
+# NO-PLUS: error: '(TEXT)(0x10)': section-relative offset "0x10" must start with '+'
+
+## Test empty offset value.
+# RUN: llvm-symbolizer --obj=%t.xcoff '(TEXT)()' 2>&1 | \
+# RUN:   FileCheck %s --check-prefix=EMPTY-OFFSET
+
+# EMPTY-OFFSET: error: '(TEXT)()': section-relative offset "" must start with '+'
+
+## Test invalid offset: decimal digits followed by non-decimal hex characters,
+## e.g. 123abc. getAsInteger with base 0 requires the whole string to be valid,
+## so this is rejected.
+# RUN: llvm-symbolizer --obj=%t.xcoff '(TEXT)(+123abc)' 2>&1 | \
+# RUN:   FileCheck %s --check-prefix=INVALID-OFFSET-HEXSUFFIX
+
+# INVALID-OFFSET-HEXSUFFIX: error: '(TEXT)(+123abc)': invalid offset "+123abc" in section-relative address
+
+## Test invalid offset: hex prefix 'x' without leading '0', e.g. xabc.
+# RUN: llvm-symbolizer --obj=%t.xcoff '(TEXT)(+xabc)' 2>&1 | \
+# RUN:   FileCheck %s --check-prefix=INVALID-OFFSET-MISSINGZERO
+
+# INVALID-OFFSET-MISSINGZERO: error: '(TEXT)(+xabc)': invalid offset "+xabc" in section-relative address
+
+## Test invalid offset: hex value with non-hex letter, e.g. 0xdefg.
+# RUN: llvm-symbolizer --obj=%t.xcoff '(TEXT)(+0xdefg)' 2>&1 | \
+# RUN:   FileCheck %s --check-prefix=INVALID-OFFSET-NONHEX
+
+# INVALID-OFFSET-NONHEX: error: '(TEXT)(+0xdefg)': invalid offset "+0xdefg" in section-relative address
+
+## Test that a leading-zero value (0123) is treated as octal (decimal 83) by
+## getAsInteger(0,...), not as decimal 123. The resolved address TEXT_BASE +
+## 83 = 0x153 has no symbol, so the output shows "??".
+# RUN: llvm-symbolizer --obj=%t.xcoff '(TEXT)(+0123)' | \
+# RUN:   FileCheck %s --check-prefix=OCTAL
+
+# OCTAL: ??
+
+## Test invalid offset value.
+# RUN: llvm-symbolizer --obj=%t.xcoff '(TEXT)(+xyz)' 2>&1 | \
+# RUN:   FileCheck %s --check-prefix=INVALID-OFFSET
+
+# INVALID-OFFSET: error: '(TEXT)(+xyz)': invalid offset "+xyz" in section-relative address
+
+## Test empty section type.
+# RUN: llvm-symbolizer --obj=%t.xcoff '()(+0x10)' 2>&1 | \
+# RUN:   FileCheck %s --check-prefix=EMPTY-SECTION
+
+# EMPTY-SECTION: error: '()(+0x10)': empty section type in section-relative address
+
+## Test error: section type not found (DATA section doesn't exist).
+# RUN: yaml2obj %s --docnum=2 -o %t.nodata.xcoff
+# RUN: llvm-symbolizer --obj=%t.nodata.xcoff '(DATA)(+0x0)' 2>&1 | \
+# RUN:   FileCheck %s --check-prefix=NO-SECTION
+
+# NO-SECTION: error: '(DATA)(+0x0)': no 'DATA' section found in XCOFF object
+
+## Test error: multiple sections of the same type.
+# RUN: yaml2obj %s --docnum=1 -D SECT2_FLAGS=STYP_TEXT -o %t.multi.xcoff
+# RUN: llvm-symbolizer --obj=%t.multi.xcoff '(TEXT)(+0x0)' 2>&1 | \
+# RUN:   FileCheck %s --check-prefix=MULTI-SECTION
+
+# MULTI-SECTION: error: '(TEXT)(+0x0)': multiple 'TEXT' sections found in XCOFF object
+
+## Test error: section type syntax with non-XCOFF object.
+# RUN: yaml2obj %s --docnum=3 -o %t.elf
+# RUN: llvm-symbolizer --obj=%t.elf '(TEXT)(+0x0)' 2>&1 | \
+# RUN:   FileCheck %s --check-prefix=NON-XCOFF
+
+# NON-XCOFF: error: '(TEXT)(+0x0)': section type syntax is only supported for XCOFF objects
+
+## Document 1: Basic XCOFF object with TEXT and DATA sections, plus an optional
+## extra section whose type is controlled by [[SECT2_FLAGS]] (default: STYP_INFO).
+## Pass -D SECT2_FLAGS=STYP_TEXT when building to create a second TEXT section,
+## which triggers the "multiple sections" error.
+--- !XCOFF
+FileHeader:
+  MagicNumber: 0x1DF
+Sections:
+  - Name:    .text
+    Address: 0x100
+    Flags:   [ STYP_TEXT ]
+    SectionData: "4E800020"
+  - Name:    .data
+    Address: 0x200
+    Flags:   [ STYP_DATA ]
+    SectionData: "00000000"
+  - Name:    .extra
+    Address: 0x300
----------------
midhuncodes7 wrote:

removed the address entry

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


More information about the llvm-commits mailing list