|
| 1 | +# RUN: yaml2obj %s -o %t |
| 2 | + |
| 3 | +# RUN: echo -n 11112222 > %t.same |
| 4 | +# RUN: echo -n 00000000 > %t.zeros |
| 5 | +# RUN: echo -n 11 > %t.short |
| 6 | +# RUN: echo -n 11113333 > %t.updated |
| 7 | +# RUN: echo -n 111122223 > %t.larger |
| 8 | + |
| 9 | +## Update the segment section with a regular chunk of data the same size as the section. |
| 10 | +# RUN: llvm-objcopy --update-section=.in_segment=%t.same %t %t.same.o |
| 11 | +# RUN: llvm-readobj --section-headers --section-data --program-headers %t.same.o | \ |
| 12 | +# RUN: FileCheck %s --check-prefix=NORMAL |
| 13 | + |
| 14 | +## Show that if we overwrite a given section (.in_segment) with new data, then rewrite it |
| 15 | +## back (from the second `--update-section`), then it should be the exact same as the |
| 16 | +## original file. |
| 17 | +# RUN: llvm-objcopy %t %t.copy.o |
| 18 | +# RUN: llvm-objcopy --update-section=.in_segment=%t.same --update-section=.in_segment=%t.zeros %t %t.original.o |
| 19 | +# RUN: cmp %t.copy.o %t.original.o |
| 20 | + |
| 21 | +## Update segment section with a smaller chunk of data. This will also update the |
| 22 | +## section size to the length of the new data written. This does not affect the offset |
| 23 | +## of any subsequent sections in the same segment as this. |
| 24 | +# RUN: llvm-objcopy --update-section=.in_segment=%t.short %t %t.short.o |
| 25 | +# RUN: llvm-readobj --section-headers --section-data --program-headers %t.short.o | \ |
| 26 | +# RUN: FileCheck %s --check-prefix=SHORT |
| 27 | + |
| 28 | +## Ensure the data that was in a shortened section within a segment is still retained. |
| 29 | +## For cases where there are gaps in segments not covered by sections, the existing |
| 30 | +## contents are preserved. |
| 31 | +# RUN: od -t x1 -j 0x78 -N 16 %t.short.o | FileCheck %s --check-prefix=PRESERVED |
| 32 | + |
| 33 | +## Add a new section via --add-section, then update it. |
| 34 | +# RUN: llvm-objcopy --add-section=.added=%t.zeros --update-section=.added=%t.updated \ |
| 35 | +# RUN: %t %t.updated.o |
| 36 | +# RUN: llvm-readobj --section-headers --section-data --program-headers %t.updated.o | \ |
| 37 | +# RUN: FileCheck %s --check-prefix=ADD-UPDATE |
| 38 | + |
| 39 | +## Adding should always be first regardless of flag order. |
| 40 | +# RUN: llvm-objcopy --update-section=.added=%t.updated --add-section=.added=%t.updated \ |
| 41 | +# RUN: %t %t.updated.o |
| 42 | +# RUN: llvm-readobj --section-headers --section-data --program-headers %t.updated.o | \ |
| 43 | +# RUN: FileCheck %s --check-prefix=ADD-UPDATE |
| 44 | + |
| 45 | +## We can't update sections which don't exist. |
| 46 | +# RUN: not llvm-objcopy --update-section=.nosection=%t.same %t %t-err1 2>&1 | \ |
| 47 | +# RUN: FileCheck %s --check-prefix=ERR-NO-SECTION |
| 48 | + |
| 49 | +## We can't update certain types of sections. |
| 50 | +# RUN: not llvm-objcopy --update-section=.nobits_type=%t.same %t %t-err2 2>&1 | \ |
| 51 | +# RUN: FileCheck %s --check-prefix=ERR-NOBITS-TYPE |
| 52 | +# RUN: not llvm-objcopy --update-section=.null_type=%t.same %t %t-err2 2>&1 | \ |
| 53 | +# RUN: FileCheck %s --check-prefix=ERR-NULL-TYPE |
| 54 | + |
| 55 | +## Fail on trying to insert data larger than the existing section. |
| 56 | +# RUN: not llvm-objcopy --update-section=.in_segment=%t.larger %t %t-err3 2>&1 | \ |
| 57 | +# RUN: FileCheck %s --check-prefix=ERR-LARGER |
| 58 | + |
| 59 | +## But we can insert larger data if the section is NOT part of a segment. |
| 60 | +# RUN: llvm-objcopy --update-section=.not_in_segment=%t.larger %t %t.larger.o |
| 61 | +# RUN: llvm-readobj --section-headers --section-data --program-headers %t.larger.o | \ |
| 62 | +# RUN: FileCheck %s --check-prefix=LONG |
| 63 | + |
| 64 | +## We should still fail on inserting larger data, even if it is superceded by a |
| 65 | +## valid --update-section flag. |
| 66 | +# RUN: not llvm-objcopy --update-section=.in_segment=%t.larger --update-section=.in_segment=%t.same %t %t-err3 2>&1 | \ |
| 67 | +# RUN: FileCheck %s --check-prefix=ERR-LARGER |
| 68 | + |
| 69 | +## Test option parsing failures. |
| 70 | +# RUN: not llvm-objcopy --update-section %t %t.out 2>&1 | FileCheck %s --check-prefix=MISSING-EQ |
| 71 | +# RUN: not llvm-objcopy --update-section=.in_segment= %t %t.out 2>&1 | FileCheck %s --check-prefix=MISSING-FILE |
| 72 | + |
| 73 | +!ELF |
| 74 | +FileHeader: |
| 75 | + Class: ELFCLASS64 |
| 76 | + Data: ELFDATA2LSB |
| 77 | + Type: ET_EXEC |
| 78 | + Machine: EM_X86_64 |
| 79 | +Sections: |
| 80 | + - Name: .in_segment |
| 81 | + Type: SHT_PROGBITS |
| 82 | + Content: "3030303030303030" |
| 83 | + - Name: .unmodified_in_segment |
| 84 | + Type: SHT_PROGBITS |
| 85 | + Content: "3130303030303030" |
| 86 | + - Name: .nobits_type |
| 87 | + Type: SHT_NOBITS |
| 88 | + - Name: .not_in_segment |
| 89 | + Type: SHT_PROGBITS |
| 90 | + - Name: .null_type |
| 91 | + Type: SHT_NULL |
| 92 | +ProgramHeaders: |
| 93 | + - Type: PT_LOAD |
| 94 | + VAddr: 0x1000 |
| 95 | + FirstSec: .in_segment |
| 96 | +## The unmodified section is for ensuring that it remains untouched (ie. its |
| 97 | +## offset is the same) even when the section before it is shrunk. |
| 98 | + LastSec: .unmodified_in_segment |
| 99 | + |
| 100 | +# NORMAL: Name: .in_segment |
| 101 | +# NORMAL: Offset: |
| 102 | +# NORMAL-SAME: {{ }}0x78{{$}} |
| 103 | +# NORMAL: Size: |
| 104 | +# NORMAL-SAME: {{ }}8{{$}} |
| 105 | +# NORMAL: SectionData ( |
| 106 | +# NORMAL-NEXT: |11112222| |
| 107 | +# NORMAL-NEXT: ) |
| 108 | +# NORMAL: Name: .unmodified_in_segment |
| 109 | +# NORMAL: Offset: |
| 110 | +# NORMAL-SAME: {{ }}0x80{{$}} |
| 111 | +# NORMAL: Size: |
| 112 | +# NORMAL-SAME: {{ }}8{{$}} |
| 113 | +# NORMAL: SectionData ( |
| 114 | +# NORMAL-NEXT: |10000000| |
| 115 | +# NORMAL-NEXT: ) |
| 116 | +# NORMAL: ProgramHeaders [ |
| 117 | +# NORMAL-NEXT: ProgramHeader { |
| 118 | +# NORMAL-NEXT: Type: PT_LOAD (0x1) |
| 119 | +# NORMAL: FileSize: |
| 120 | +# NORMAL-SAME: {{ }}16{{$}} |
| 121 | +# NORMAL: MemSize: |
| 122 | +# NORMAL-SAME: {{ }}16{{$}} |
| 123 | +# NORMAL: } |
| 124 | +# NORMAL-NEXT: ] |
| 125 | + |
| 126 | +# SHORT: Name: .in_segment |
| 127 | +# SHORT: Offset: |
| 128 | +# SHORT-SAME: {{ }}0x78{{$}} |
| 129 | +# SHORT: Size: |
| 130 | +# SHORT-SAME: {{ }}2{{$}} |
| 131 | +# SHORT: SectionData ( |
| 132 | +# SHORT-NEXT: |11| |
| 133 | +# SHORT-NEXT: ) |
| 134 | +# SHORT: Name: .unmodified_in_segment |
| 135 | +# SHORT: Offset: |
| 136 | +# SHORT-SAME: {{ }}0x80{{$}} |
| 137 | +# SHORT: Size: |
| 138 | +# SHORT-SAME: {{ }}8{{$}} |
| 139 | +# SHORT: SectionData ( |
| 140 | +# SHORT-NEXT: |10000000| |
| 141 | +# SHORT-NEXT: ) |
| 142 | +# SHORT: ProgramHeaders [ |
| 143 | +# SHORT-NEXT: ProgramHeader { |
| 144 | +# SHORT-NEXT: Type: PT_LOAD (0x1) |
| 145 | +# SHORT: FileSize: |
| 146 | +# SHORT-SAME: {{ }}16{{$}} |
| 147 | +# SHORT: MemSize: |
| 148 | +# SHORT-SAME: {{ }}16{{$}} |
| 149 | +# SHORT: } |
| 150 | +# SHORT-NEXT: ] |
| 151 | + |
| 152 | +## The first 8 bytes are the modified section. The last 8 bytes are the |
| 153 | +## unmodified section. |
| 154 | +# PRESERVED: 31 31 30 30 30 30 30 30 31 30 30 30 30 30 30 30 |
| 155 | + |
| 156 | +# LONG: Name: .not_in_segment |
| 157 | +# LONG: Size: |
| 158 | +# LONG-SAME: {{ }}9{{$}} |
| 159 | +# LONG: SectionData ( |
| 160 | +# LONG-NEXT: |111122223| |
| 161 | +# LONT-NEXT: ) |
| 162 | + |
| 163 | +# ADD-UPDATE: Name: .added |
| 164 | +# ADD-UPDATE: Size: |
| 165 | +# ADD-UPDATE-SAME: {{ }}8{{$}} |
| 166 | +# ADD-UPDATE: SectionData ( |
| 167 | +# ADD-UPDATE-NEXT: |11113333| |
| 168 | +# ADD-UPDATE: ) |
| 169 | + |
| 170 | +# ERR-NO-SECTION: error: {{.*}}section '.nosection' not found |
| 171 | +# ERR-NOBITS-TYPE: error: {{.*}}section '.nobits_type' can't be updated because it does not have contents |
| 172 | +# ERR-NULL-TYPE: error: {{.*}}section '.null_type' can't be updated because it does not have contents |
| 173 | +# ERR-LARGER: error: {{.*}}cannot fit data of size 9 into section '.in_segment' with size 8 that is part of a segment |
| 174 | + |
| 175 | +# MISSING-EQ: error: bad format for --update-section: missing '=' |
| 176 | +# MISSING-FILE: error: bad format for --update-section: missing file name |
0 commit comments