[llvm] Fix MachO exports trie parsing.
authorJuergen Ributzka <juergen@apple.com>
Mon, 13 Jun 2022 22:57:51 +0000 (15:57 -0700)
committerJuergen Ributzka <juergen@apple.com>
Wed, 15 Jun 2022 16:28:58 +0000 (09:28 -0700)
The exports trie parser ordinal validation check doesn't consider the case where
the ordinal can be zero or negative for certain special values that are defined
in BindSpecialDylib. Update the validation to account for that fact and add a
test case.

This fixes rdar://94844233.

Differential Revision: https://reviews.llvm.org/D127806

llvm/lib/Object/MachOObjectFile.cpp
llvm/test/Object/AArch64/nm-flat-reexport.test [new file with mode: 0644]
llvm/test/Object/Inputs/MachO/flat-reexport.yaml [new file with mode: 0644]

index e408062..2f463a1 100644 (file)
@@ -2997,7 +2997,9 @@ void ExportEntry::pushNode(uint64_t offset) {
         return;
       }
       if (O != nullptr) {
-        if (State.Other > O->getLibraryCount()) {
+        // Only positive numbers represent library ordinals. Zero and negative
+        // numbers have special meaning (see BindSpecialDylib).
+        if ((int64_t)State.Other > 0 && State.Other > O->getLibraryCount()) {
           *E = malformedError(
               "bad library ordinal: " + Twine((int)State.Other) + " (max " +
               Twine((int)O->getLibraryCount()) +
diff --git a/llvm/test/Object/AArch64/nm-flat-reexport.test b/llvm/test/Object/AArch64/nm-flat-reexport.test
new file mode 100644 (file)
index 0000000..6c364a5
--- /dev/null
@@ -0,0 +1,7 @@
+RUN: rm -rf %t && mkdir -p %t
+RUN: yaml2obj %p/../Inputs/MachO/flat-reexport.yaml -o %t/flat-reexport
+RUN: llvm-nm --dyldinfo-only %t/flat-reexport | FileCheck %s
+
+CHECK-NOT: error
+CHECK: _fputc
+CHECK: _sym1
\ No newline at end of file
diff --git a/llvm/test/Object/Inputs/MachO/flat-reexport.yaml b/llvm/test/Object/Inputs/MachO/flat-reexport.yaml
new file mode 100644 (file)
index 0000000..7b5015d
--- /dev/null
@@ -0,0 +1,148 @@
+# echo "_fputc" >> reexports.exp
+# echo "void sym1(void){}" | xcrun clang --target=arm64-apple-macos11.0 -o flat-reexport -dynamiclib -Wl,-reexported_symbols_list,reexports.exp -Wl,-flat_namespace -x c -
+# obj2yaml --raw-segment=data --raw-segment=linkedit flat-reexport > flat-reexport.yaml
+--- !mach-o
+FileHeader:
+  magic:           0xFEEDFACF
+  cputype:         0x100000C
+  cpusubtype:      0x0
+  filetype:        0x6
+  ncmds:           13
+  sizeofcmds:      672
+  flags:           0x108004
+  reserved:        0x0
+LoadCommands:
+  - cmd:             LC_SEGMENT_64
+    cmdsize:         232
+    segname:         __TEXT
+    vmaddr:          0
+    vmsize:          16384
+    fileoff:         0
+    filesize:        16384
+    maxprot:         5
+    initprot:        5
+    nsects:          2
+    flags:           0
+    Sections:
+      - sectname:        __text
+        segname:         __TEXT
+        addr:            0x3FB4
+        size:            4
+        offset:          0x3FB4
+        align:           2
+        reloff:          0x0
+        nreloc:          0
+        flags:           0x80000400
+        reserved1:       0x0
+        reserved2:       0x0
+        reserved3:       0x0
+        content:         C0035FD6
+      - sectname:        __unwind_info
+        segname:         __TEXT
+        addr:            0x3FB8
+        size:            72
+        offset:          0x3FB8
+        align:           2
+        reloff:          0x0
+        nreloc:          0
+        flags:           0x0
+        reserved1:       0x0
+        reserved2:       0x0
+        reserved3:       0x0
+        content:         010000001C000000000000001C000000000000001C00000002000000B43F00003400000034000000B93F00000000000034000000030000000C000100100001000000000000000002
+  - cmd:             LC_SEGMENT_64
+    cmdsize:         72
+    segname:         __LINKEDIT
+    vmaddr:          16384
+    vmsize:          16384
+    fileoff:         16384
+    filesize:        442
+    maxprot:         1
+    initprot:        1
+    nsects:          0
+    flags:           0
+  - cmd:             LC_ID_DYLIB
+    cmdsize:         40
+    dylib:
+      name:            24
+      timestamp:       1
+      current_version: 0
+      compatibility_version: 0
+    Content:         flat-reexport
+    ZeroPadBytes:    3
+  - cmd:             LC_DYLD_INFO_ONLY
+    cmdsize:         48
+    rebase_off:      0
+    rebase_size:     0
+    bind_off:        0
+    bind_size:       0
+    weak_bind_off:   0
+    weak_bind_size:  0
+    lazy_bind_off:   0
+    lazy_bind_size:  0
+    export_off:      16384
+    export_size:     40
+  - cmd:             LC_SYMTAB
+    cmdsize:         24
+    symoff:          16432
+    nsyms:           4
+    stroff:          16496
+    strsize:         40
+  - cmd:             LC_DYSYMTAB
+    cmdsize:         80
+    ilocalsym:       0
+    nlocalsym:       0
+    iextdefsym:      0
+    nextdefsym:      2
+    iundefsym:       2
+    nundefsym:       2
+    tocoff:          0
+    ntoc:            0
+    modtaboff:       0
+    nmodtab:         0
+    extrefsymoff:    0
+    nextrefsyms:     0
+    indirectsymoff:  0
+    nindirectsyms:   0
+    extreloff:       0
+    nextrel:         0
+    locreloff:       0
+    nlocrel:         0
+  - cmd:             LC_UUID
+    cmdsize:         24
+    uuid:            840B0A4F-3749-37F1-B52C-37DC65AAA0DB
+  - cmd:             LC_BUILD_VERSION
+    cmdsize:         32
+    platform:        1
+    minos:           720896
+    sdk:             787712
+    ntools:          1
+    Tools:
+      - tool:            3
+        version:         53280768
+  - cmd:             LC_SOURCE_VERSION
+    cmdsize:         16
+    version:         0
+  - cmd:             LC_LOAD_DYLIB
+    cmdsize:         56
+    dylib:
+      name:            24
+      timestamp:       2
+      current_version: 85948417
+      compatibility_version: 65536
+    Content:         '/usr/lib/libSystem.B.dylib'
+    ZeroPadBytes:    6
+  - cmd:             LC_FUNCTION_STARTS
+    cmdsize:         16
+    dataoff:         16424
+    datasize:        8
+  - cmd:             LC_DATA_IN_CODE
+    cmdsize:         16
+    dataoff:         16432
+    datasize:        0
+  - cmd:             LC_CODE_SIGNATURE
+    cmdsize:         16
+    dataoff:         16544
+    datasize:        282
+__LINKEDIT:      00015F0005000273796D310014667075746300190300B47F000C08FEFFFFFFFFFFFFFFFF01000000B47F000000000000020000000B0000000200000000000000090000000F010000B43F0000000000000F0000000100000000000000000000001600000001000000000000000000000020005F6670757463005F73796D31005F66707574630064796C645F737475625F62696E64657200000000000000000000FADE0CC00000011A000000010000000000000014FADE0C0200000106000204000002000200000066000000580000000000000005000040A02002000C000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000666C61742D72656578706F727400865F77AB8871951DEC5737A30B021C8E233E3E095F50DBA58B565C3D6E2430FBAD7FACB2586FC6E966C004D7D1D16B024F5805FF7CB47C7A85DABD8B48892CA7AD7FACB2586FC6E966C004D7D1D16B024F5805FF7CB47C7A85DABD8B48892CA7A54BC24923D5539A8353B9794289E00284149CD938F194E3682F4E35DED490FA8992AD48884D239DE460B5F73DE2583146FF7DA8718E7AD5F985E9464529B170
+...