llvm.org GIT mirror llvm / 7492d68
[llvm-objcopy] Add file names to error messages Summary: This patch adds the file names to llvm-objcopy error messages. It makes easy to identify which file causes an error. Bugzilla: https://bugs.llvm.org/show_bug.cgi?id=41798 Reviewers: espindola, alexshap, rupprecht, jhenderson, jakehehrlich Reviewed By: rupprecht, jhenderson, jakehehrlich Subscribers: emaste, arichardson, jakehehrlich, MaskRay, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D61993 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@361450 91177308-0d34-0410-b5e6-96231b3b80d8 Seiya Nuta 4 months ago
11 changed file(s) with 56 addition(s) and 40 deletion(s). Raw diff Collapse all Expand all
22 # RUN: llvm-objcopy -O binary --only-section .text %t %t3
33 # RUN: llvm-objcopy --dump-section .text=%t4 %t %t5
44 # RUN: llvm-objcopy --dump-section .foo=%t6 %t %t7
5 # RUN: not llvm-objcopy --dump-section .bar=%t8 %t %t9 2>&1 | FileCheck %s --check-prefix=NOBITS
5 # RUN: not llvm-objcopy --dump-section .bar=%t8 %t %t9 2>&1 | FileCheck %s --check-prefix=NOBITS -DINPUT=%t
66 # RUN: od -t x1 %t2 | FileCheck %s
77 # RUN: od -t x1 %t6 | FileCheck %s --check-prefix=NON-ALLOC
88 # RUN: wc -c %t2 | FileCheck %s --check-prefix=SIZE
4040
4141 #SIZE: 4
4242
43 #NOBITS: cannot dump section '.bar': it has no contents
43 #NOBITS: error: '[[INPUT]]': cannot dump section '.bar': it has no contents
11
22 ## Check we cannot remove the .dynsym symbol table because dynamic
33 ## relocation section .rela.dyn still references it via sh_link field.
4 # RUN: not llvm-objcopy -R .dynsym %t %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR
5 # ERR: error: symbol table '.dynsym' cannot be removed because it is referenced by the relocation section '.rela.dyn'
4 # RUN: not llvm-objcopy -R .dynsym %t %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR -DINPUT=%t
5 # ERR: error: '[[INPUT]]': symbol table '.dynsym' cannot be removed because it is referenced by the relocation section '.rela.dyn'
66
77 ## Check we can remove .dynsym after removing the reference.
88 # RUN: llvm-objcopy -R .dynsym -R .rela.dyn %t %t2
None # RUN: not llvm-objcopy -R .dynstr %p/Inputs/dynsym.so %t 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR
0 # RUN: not llvm-objcopy -R .dynstr %p/Inputs/dynsym.so %t 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR1 -DINPUT=%p/Inputs/dynsym.so
11 # RUN: cp %p/Inputs/dynsym.so %t2
2 # RUN: not llvm-strip --no-strip-all -R .dynstr %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR
2 # RUN: not llvm-strip --no-strip-all -R .dynstr %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR2 -DINPUT=%t2
33
4 # ERR: section '.dynstr' cannot be removed because it is referenced by the section '.dynsym'
4 # ERR1: error: '[[INPUT]]': section '.dynstr' cannot be removed because it is referenced by the section '.dynsym'
5 # ERR2: error: '[[INPUT]]': section '.dynstr' cannot be removed because it is referenced by the section '.dynsym'
56
67 # RUN: llvm-objcopy --allow-broken-links -R .dynstr %p/Inputs/dynsym.so %t3
78 # RUN: llvm-objdump --section-headers %t3 | FileCheck %s --check-prefix=SECTIONS --implicit-check-not=.dynstr
0 # RUN: yaml2obj %s > %t
1 # RUN: not llvm-objcopy --build-id-link-dir=%t-dir --build-id-link-input=.debug %t 2>&1 >/dev/null | FileCheck %s
1 # RUN: not llvm-objcopy --build-id-link-dir=%t-dir --build-id-link-input=.debug %t 2>&1 >/dev/null | FileCheck %s -DINPUT=%t
22
3 # CHECK: could not find build ID
3 # CHECK: error: '[[INPUT]]': could not find build ID
44
55 --- !ELF
66 FileHeader:
0 # RUN: yaml2obj %s > %t
1 # RUN: not llvm-objcopy -R .symtab %t %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR
1 # RUN: not llvm-objcopy -R .symtab %t %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR1 -DINPUT=%t
22 # RUN: cp %t %t3
3 # RUN: not llvm-strip --no-strip-all -R .symtab %t3 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR
3 # RUN: not llvm-strip --no-strip-all -R .symtab %t3 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR2 -DINPUT=%t3
44
55 !ELF
66 FileHeader:
3030 Size: 4
3131 Binding: STB_GLOBAL
3232
33 # ERR: symbol table '.symtab' cannot be removed because it is referenced by the relocation section '.rel.text'
33 # ERR1: error: '[[INPUT]]': symbol table '.symtab' cannot be removed because it is referenced by the relocation section '.rel.text'
34 # ERR2: error: '[[INPUT]]': symbol table '.symtab' cannot be removed because it is referenced by the relocation section '.rel.text'
3435
3536 # RUN: llvm-objcopy --allow-broken-links -R .symtab %t %t4
3637 # RUN: llvm-readobj --sections %t4 | FileCheck %s --check-prefix=SECTIONS --implicit-check-not=.symtab
0 # RUN: yaml2obj %s -o %t.o
1 # RUN: not llvm-objcopy -R .foo %t.o %t1 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR
1 # RUN: not llvm-objcopy -R .foo %t.o %t1 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR1 -DINPUT=%t.o
22 # RUN: cp %t.o %t2
3 # RUN: not llvm-strip --no-strip-all -R .foo %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR
3 # RUN: not llvm-strip --no-strip-all -R .foo %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR2 -DINPUT=%t2
44
55 --- !ELF
66 FileHeader:
1515 Type: SHT_PROGBITS
1616 Link: .foo
1717
18 # ERR: section '.foo' cannot be removed because it is referenced by the section '.bar'
18 # ERR1: error: '[[INPUT]]': section '.foo' cannot be removed because it is referenced by the section '.bar'
19 # ERR2: error: '[[INPUT]]': section '.foo' cannot be removed because it is referenced by the section '.bar'
1920
2021 # RUN: llvm-objcopy --allow-broken-links -R .foo %t.o %t3
2122 # RUN: llvm-readobj --sections %t3 | FileCheck %s --check-prefix=SECTIONS --implicit-check-not=.foo
0 # RUN: yaml2obj %s > %t
1 # RUN: not llvm-objcopy -R .shstrtab %t %t2 2>&1 >/dev/null | FileCheck %s
1 # RUN: not llvm-objcopy -R .shstrtab %t %t2 2>&1 >/dev/null | FileCheck %s -DINPUT=%t
22
33 !ELF
44 FileHeader:
77 Type: ET_REL
88 Machine: EM_X86_64
99
10 # CHECK: cannot write section header table because section header string table was removed
10 # CHECK: error: '[[INPUT]]': cannot write section header table because section header string table was removed
11 ## referenced by relocations contained in the object.
22
33 # RUN: yaml2obj %s > %t1
4 # RUN: not llvm-objcopy -R .data %t1 2>&1 | FileCheck %s
5 # CHECK: error: section '.data' cannot be removed: (.text+0x1) has relocation against symbol 'foo'
4 # RUN: not llvm-objcopy -R .data %t1 2>&1 | FileCheck %s -DINPUT=%t1
5 # CHECK: error: '[[INPUT]]': section '.data' cannot be removed: (.text+0x1) has relocation against symbol 'foo'
66
77 ## Check the behavior when we also remove the relocation section.
88 ## We have no reference in this case and hence no error should be emitted.
1919 # STRIPSYM-NOT: bar
2020 # STRIPSYM-NOT: .rela.text
2121
22 # RUN: not llvm-objcopy --strip-symbol=bar %t %t2 2>&1 | FileCheck %s --check-prefix=ERR
23 # ERR: not stripping symbol 'bar' because it is named in a relocation
22 # RUN: not llvm-objcopy --strip-symbol=bar %t %t2 2>&1 | FileCheck %s --check-prefix=ERR -DINPUT=%t
23 # ERR: error: '[[INPUT]]': not stripping symbol 'bar' because it is named in a relocation
2424
2525 --- !ELF
2626 FileHeader:
0 # RUN: yaml2obj %s > %t
1 # RUN: not llvm-objcopy -R .strtab %t %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR
1 # RUN: not llvm-objcopy -R .strtab %t %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR1 -DINPUT=%t
22 # RUN: cp %t %t3
3 # RUN: not llvm-strip --no-strip-all -R .strtab %t3 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR
3 # RUN: not llvm-strip --no-strip-all -R .strtab %t3 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR2 -DINPUT=%t3
44
55 !ELF
66 FileHeader:
99 Type: ET_REL
1010 Machine: EM_X86_64
1111
12 # ERR: string table '.strtab' cannot be removed because it is referenced by the symbol table '.symtab'
12 # ERR1: error: '[[INPUT]]': string table '.strtab' cannot be removed because it is referenced by the symbol table '.symtab'
13 # ERR2: error: '[[INPUT]]': string table '.strtab' cannot be removed because it is referenced by the symbol table '.symtab'
1314
1415 # RUN: llvm-objcopy --allow-broken-links -R .strtab %t %t4
1516 # RUN: llvm-objdump --section-headers %t4 | FileCheck %s --check-prefix=SECTIONS --implicit-check-not=.strtab
155155
156156 template
157157 static Expected>
158 findBuildID(const object::ELFFile &In) {
159 for (const auto &Phdr : unwrapOrError(In.program_headers())) {
158 findBuildID(const CopyConfig &Config, const object::ELFFile &In) {
159 auto PhdrsOrErr = In.program_headers();
160 if (auto Err = PhdrsOrErr.takeError())
161 return createFileError(Config.InputFilename, std::move(Err));
162
163 for (const auto &Phdr : *PhdrsOrErr) {
160164 if (Phdr.p_type != PT_NOTE)
161165 continue;
162166 Error Err = Error::success();
164168 if (Note.getType() == NT_GNU_BUILD_ID && Note.getName() == ELF_NOTE_GNU)
165169 return Note.getDesc();
166170 if (Err)
167 return std::move(Err);
168 }
169 return createStringError(llvm::errc::invalid_argument,
170 "could not find build ID");
171 return createFileError(Config.InputFilename, std::move(Err));
172 }
173
174 return createFileError(
175 Config.InputFilename,
176 createStringError(llvm::errc::invalid_argument,
177 "could not find build ID"));
171178 }
172179
173180 static Expected>
174 findBuildID(const object::ELFObjectFileBase &In) {
181 findBuildID(const CopyConfig &Config, const object::ELFObjectFileBase &In) {
175182 if (auto *O = dyn_cast>(&In))
176 return findBuildID(*O->getELFFile());
183 return findBuildID(Config, *O->getELFFile());
177184 else if (auto *O = dyn_cast>(&In))
178 return findBuildID(*O->getELFFile());
185 return findBuildID(Config, *O->getELFFile());
179186 else if (auto *O = dyn_cast>(&In))
180 return findBuildID(*O->getELFFile());
187 return findBuildID(Config, *O->getELFFile());
181188 else if (auto *O = dyn_cast>(&In))
182 return findBuildID(*O->getELFFile());
189 return findBuildID(Config, *O->getELFFile());
183190
184191 llvm_unreachable("Bad file format");
185192 }
680687 StringRef SecName = SecPair.first;
681688 StringRef File = SecPair.second;
682689 if (Error E = dumpSectionToFile(SecName, File, Obj))
683 return createFileError(Config.InputFilename, std::move(E));
690 return E;
684691 }
685692
686693 if (!Config.AddGnuDebugLink.empty())
729736 ArrayRef BuildIdBytes;
730737
731738 if (!Config.BuildIdLinkDir.empty()) {
732 BuildIdBytes = unwrapOrError(findBuildID(In));
739 auto BuildIdBytesOrErr = findBuildID(Config, In);
740 if (auto E = BuildIdBytesOrErr.takeError())
741 return E;
742 BuildIdBytes = *BuildIdBytesOrErr;
743
733744 if (BuildIdBytes.size() < 2)
734745 return createFileError(
735746 Config.InputFilename,
744755 return E;
745756
746757 if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType))
747 return E;
758 return createFileError(Config.InputFilename, std::move(E));
759
748760 std::unique_ptr Writer =
749761 createWriter(Config, *Obj, Out, OutputElfType);
750762 if (Error E = Writer->finalize())
751 return E;
763 return createFileError(Config.InputFilename, std::move(E));
752764 if (Error E = Writer->write())
753765 return E;
754766 if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkOutput)
755767 if (Error E =
756768 linkToBuildIdDir(Config, Config.OutputFilename,
757769 Config.BuildIdLinkOutput.getValue(), BuildIdBytes))
758 return E;
770 return createFileError(Config.OutputFilename, std::move(E));
759771
760772 return Error::success();
761773 }