129 Commits

Author SHA1 Message Date
Puyan Lotfi
41306baa27 [NFC][llvm-objcopy] Make helper functions static
Anywhere in tools/llvm-objcopy where functions or classes are not referenced
outside of a given file, we change things to make the function or class static
or put inside an anonymous namespace.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@337220 91177308-0d34-0410-b5e6-96231b3b80d8
2018-07-16 22:17:05 +00:00
Jake Ehrlich
51e4fb6e38 [llvm-objcopy] Add support for large indexes
This patch is an update of an older patch that never landed
(see here: https://reviews.llvm.org/D42516)

Recently various users have run into this issue and it just 100%
has to be solved at this point. The main difference in this patch
is that I use gunzip instead of unzip which should hopefully allow
tests to pass. Please review this as if it is a new patch however.
I found some issues along the way and made some minor modifications.

The binary used in this patch for testing (a zip file to make it small)
can be found here:
https://drive.google.com/file/d/1UjsnTO9edLttZibbr-2T1bJl92KEQFAO/view?usp=sharing

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@337204 91177308-0d34-0410-b5e6-96231b3b80d8
2018-07-16 19:48:52 +00:00
Stephen Hines
df4581eddc Add --strip-all option back to llvm-strip.
Summary:
This option appears to have been dropped as part of the refactoring in
r331663. Unfortunately, if we want to use llvm-strip as a drop-in
replacement for strip, this option should still be available.

Reviewers: alexshap

Reviewed By: alexshap

Subscribers: meikeb, kongyi, chh, jakehehrlich, llvm-commits, pirama

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@336921 91177308-0d34-0410-b5e6-96231b3b80d8
2018-07-12 17:42:17 +00:00
Alexander Shaposhnikov
80e7ea0a47 [llvm-objcopy] Add support for static libraries
This diff adds support for handling static libraries 
to llvm-objcopy and llvm-strip.

Test plan: make check-all

Differential revision: https://reviews.llvm.org/D48413


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@336455 91177308-0d34-0410-b5e6-96231b3b80d8
2018-07-06 17:51:03 +00:00
Alexander Shaposhnikov
89be48cd83 [llvm-objcopy] Remove unused field from Object
The class Object contains std::shared_ptr<MemoryBuffer> OwnedData
which is not used anywhere. Besides avoiding two stage initialization 
the motivation to remove it comes from the plan to add (currently missing) support 
for static libraries.
NFC.

Test plan: make check-all

Differential revision: https://reviews.llvm.org/D47855


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@334217 91177308-0d34-0410-b5e6-96231b3b80d8
2018-06-07 19:41:42 +00:00
Paul Semel
f36d22b003 [llvm-strip] Expose --strip-unneeded option
Differential Revision: https://reviews.llvm.org/D47818

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@334182 91177308-0d34-0410-b5e6-96231b3b80d8
2018-06-07 10:05:25 +00:00
Alexander Shaposhnikov
ad593b0363 [llvm-strip] Expose --discard-all option
Expose objcopy's --discard-all option in llvm-strip.

Test plan: make check-all

Differential revision: https://reviews.llvm.org/D47750


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@334131 91177308-0d34-0410-b5e6-96231b3b80d8
2018-06-06 21:23:19 +00:00
Alexander Shaposhnikov
711621feb7 [llvm-strip] Add missing aliases for --strip-debug
Add missing aliases for --strip-debug: -g, -S, -d.

Test plan: make check-all

Differential revision: https://reviews.llvm.org/D47674


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@333940 91177308-0d34-0410-b5e6-96231b3b80d8
2018-06-04 18:55:41 +00:00
Paul Semel
64284524f0 [llvm-objcopy] Fix null symbol handling
This fixes the bug where strip-all option was
leading to a malformed outputted ELF file.

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@333772 91177308-0d34-0410-b5e6-96231b3b80d8
2018-06-01 16:19:46 +00:00
Alexander Shaposhnikov
9eb9307162 [llvm-strip] Add -o option to llvm-strip
This diff implements the option -o 
for specifying a file to write the output to.

Test plan: make check-all

Differential revision: https://reviews.llvm.org/D47505


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@333693 91177308-0d34-0410-b5e6-96231b3b80d8
2018-05-31 20:42:13 +00:00
Peter Collingbourne
44e7cf45f6 llvm-objcopy: Set sh_link to 0 on unrecognized symtab-linked sections.
Per discussion on the generic-abi mailing list:
https://groups.google.com/forum/#!topic/generic-abi/MPr8TVtnVn4

An object file manipulation tool must either write out a symbol
table with the same number of entries as the original symbol table
and in the same order, or if this is impossible, refuse to operate
on the object file if it has unrecognized sections that are linked
to the symtab section. However, existing tools (namely GNU strip,
GNU objcopy and ld.{bfd,gold,lld} -r) do not comply with this at
present: they change symbol table indexes and set sh_link to 0 on
the unrecognized symtab-linked sections.

We intend to use the latter as a (temporary) signal that a tool has
operated on a proposed new symtab-linked section and invalidated the
symbol table indexes. However, llvm-objcopy currently keeps sh_link
pointing to the new symtab section. This patch changes llvm-objcopy
to set sh_link to 0 to match the behaviour of the other tools.

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@333581 91177308-0d34-0410-b5e6-96231b3b80d8
2018-05-30 19:30:39 +00:00
Paul Semel
5af80c626e [llvm-objcopy] Add --keep-file-symbols option
This option prevent from removing file symbols while removing symbols.

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@333339 91177308-0d34-0410-b5e6-96231b3b80d8
2018-05-26 08:10:37 +00:00
Paul Semel
fe9deeb432 [llvm-objcopy] Add --strip-unneeded option
Differential Revision: https://reviews.llvm.org/D46896

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@333267 91177308-0d34-0410-b5e6-96231b3b80d8
2018-05-25 11:01:25 +00:00
Alexander Shaposhnikov
0390dacdd0 [llvm-strip] Minor fix of the usage of TableGen
This is a small follow-up to the revisions r333117 and r331663.

1. Avoid the name conflicts of the generated variables for prefixes.
2. Apply clang-format -i -style=llvm to llvm-objcopy.cpp once again.
3. Add a test for the flag with double dash.

Test plan: make check-all


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@333120 91177308-0d34-0410-b5e6-96231b3b80d8
2018-05-23 20:39:52 +00:00
Alexander Shaposhnikov
49011d2b65 [llvm-strip] Expose --keep-symbol option
Expose --keep-symbol option in llvm-strip.

Test plan: make check-all

Differential revision: https://reviews.llvm.org/D47222


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@333117 91177308-0d34-0410-b5e6-96231b3b80d8
2018-05-23 19:44:19 +00:00
Alexander Shaposhnikov
8eff37b3dd [llvm-objcopy] Fix the behavior of --strip-* and --keep-symbol
If one runs llvm-objcopy --strip-all --keep-symbol foo
and the symbol table indeed contains the symbol "foo"
then it should not be removed.

Test plan: make check-all

Differential revision: https://reviews.llvm.org/D47052


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@333008 91177308-0d34-0410-b5e6-96231b3b80d8
2018-05-22 18:24:07 +00:00
Paul Semel
7d79fc6afb Revert "[llvm-objcopy] Add --strip-unneeded option"
There is a use after free I didn't see. Need to investigate.

This reverts commit f7624abeb1f0d012309baf2e78cf2499fbfe5e5f.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@332925 91177308-0d34-0410-b5e6-96231b3b80d8
2018-05-22 01:04:36 +00:00
Paul Semel
f7624abeb1 [llvm-objcopy] Add --strip-unneeded option
This option removes symbols that are not needed by relocations.

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@332915 91177308-0d34-0410-b5e6-96231b3b80d8
2018-05-21 22:50:32 +00:00
Alexander Shaposhnikov
2135b33dc5 [llvm-objcopy] Fix formatting
Apply clang-format -i -style=llvm to llvm-objcopy.cpp
NFC.

Test plan: make check-all


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@332693 91177308-0d34-0410-b5e6-96231b3b80d8
2018-05-18 04:18:41 +00:00
Jake Ehrlich
bcc9a743a6 [llvm-objcopy] Add --only-keep-debug as a noop
This option just keeps being a problem and really needs to be implemented
in some fashion. Implementing it properly requires some kind of
"replaceSectionReference" method because all the existing links need to be
maintained. The desired behavior is just for allocated sections to become
NOBITS but actually implementing that is rather tricky due to the current
design of llvm-objcopy. However converting allocated sections to NOBITS is
just an optimization and not something debuggers need. Debuggers can debug
a stripped executable and take an unstripped executable for that stripped
executable as input. Additionally allocated sections account for a very
small part of debug binaries so this optimization is quite small. I propose
that for the time being we implement this as a NOP so that people can use
llvm-objcopy where they need to, just in a sub-optimal way.

This option has already blocked a lot of people and its currently blocking me.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@332396 91177308-0d34-0410-b5e6-96231b3b80d8
2018-05-15 20:53:53 +00:00
Paul Semel
5fe8b27775 [llvm-objcopy] Add --keep-symbol (-K) option
This option permits to explicitly keep the specified
symbol so that it doesn't get removed.

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@332356 91177308-0d34-0410-b5e6-96231b3b80d8
2018-05-15 14:09:37 +00:00
Alexander Shaposhnikov
3e6c331c76 [llvm-strip] Add support for -remove-section
This diff adds support for -remove-section to llvm-strip.

Test plan: make check-all

Differential revision: https://reviews.llvm.org/D46567


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@332081 91177308-0d34-0410-b5e6-96231b3b80d8
2018-05-11 05:27:06 +00:00
Paul Semel
dd5a608d8b [llvm-objcopy] Add --strip-symbol (-N) option
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@331924 91177308-0d34-0410-b5e6-96231b3b80d8
2018-05-09 21:36:54 +00:00
Alexander Shaposhnikov
680a6e84e3 [llvm-objcopy] Fix exit code
Set the exit code to 1 if no arguments are specified.

Test plan: make check-all

Differential revision: https://reviews.llvm.org/D46547



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@331776 91177308-0d34-0410-b5e6-96231b3b80d8
2018-05-08 17:12:54 +00:00
Alexander Shaposhnikov
409ea28c47 [tools] Introduce llvm-strip
llvm-strip is supposed to be a drop-in replacement for binutils strip.
To start the ball rolling this diff adds the initial bits for llvm-strip,
more features will be added incrementally over time.

Test plan: make check-all

Differential revision: https://reviews.llvm.org/D46407



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@331663 91177308-0d34-0410-b5e6-96231b3b80d8
2018-05-07 19:32:09 +00:00
Paul Semel
80e28a9c4e [llvm-objcopy] Add --discard-all (-x) option
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@331400 91177308-0d34-0410-b5e6-96231b3b80d8
2018-05-02 20:19:22 +00:00
Paul Semel
b4db044c01 [llvm-objcopy] Add --weaken option
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@331397 91177308-0d34-0410-b5e6-96231b3b80d8
2018-05-02 20:14:49 +00:00
Paul Semel
0fb24c145b [llvm-objcopy] Add --weaken-symbol (-W) option
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@331070 91177308-0d34-0410-b5e6-96231b3b80d8
2018-04-27 19:16:27 +00:00
Paul Semel
384d8a7924 [llvm-objcopy] Add --globalize-symbol option
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@331068 91177308-0d34-0410-b5e6-96231b3b80d8
2018-04-27 19:09:44 +00:00
Alexander Shaposhnikov
5b92658eab [llvm-objcopy] Implement --redefine-sym option
This diff implements --redefine-sym option 
for changing the name of a symbol.

Test plan: make check-all

Differential revision: https://reviews.llvm.org/D46029



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@330973 91177308-0d34-0410-b5e6-96231b3b80d8
2018-04-26 18:28:17 +00:00
Paul Semel
37dc08589c [llvm-objcopy] Add --localize-symbol option
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@330963 91177308-0d34-0410-b5e6-96231b3b80d8
2018-04-26 17:44:43 +00:00
Alexander Shaposhnikov
86e361fbbc [llvm-objcopy] Adjust the help message
Capitalize the first letter, 
make the text a bit more consistent.
NFC.

Differential revision: https://reviews.llvm.org/D46025



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@330777 91177308-0d34-0410-b5e6-96231b3b80d8
2018-04-24 21:44:13 +00:00
Alexander Shaposhnikov
7f71e97394 [llvm-objcopy] Adjust the code for the old versions of msvc
Follow-up for r330685.




git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@330686 91177308-0d34-0410-b5e6-96231b3b80d8
2018-04-24 06:23:22 +00:00
Alexander Shaposhnikov
ae1ca02d14 Recommit "[llvm-objcopy] Switch over to using TableGen for parsing arguments"
Add explicit dependency on ObjcopyTableGen 
and rerun the tests on Windows. 
I will double-check the build bots 
and revert this commit if necessary.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@330685 91177308-0d34-0410-b5e6-96231b3b80d8
2018-04-24 05:43:32 +00:00
Alexander Shaposhnikov
f8bff0d651 [llvm-objcopy] Fix sh_link
This diff fixes sh_link for various types of sections 
(i.e. for SHT_ARM_EXIDX, SHT_HASH). In particular, this change enables us
to use llvm-objcopy with clang -gsplit-dwarf for the target android-arm.

Test plan: make check-all

Differential revision: https://reviews.llvm.org/D45851



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@330478 91177308-0d34-0410-b5e6-96231b3b80d8
2018-04-20 20:46:04 +00:00
Rui Ueyama
0b9d56a30e Define InitLLVM to do common initialization all at once.
We have a few functions that virtually all command wants to run on
process startup/shutdown. This patch adds InitLLVM class to do that
all at once, so that we don't need to copy-n-paste boilerplate code
to each llvm command's main() function.

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@330046 91177308-0d34-0410-b5e6-96231b3b80d8
2018-04-13 18:26:06 +00:00
Jake Ehrlich
b1f2b2d1ce Revert "[llvm-objcopy] Switch over to using TableGen for parsing arguments"
TableGen seems to work differently on windows. I'll need to revert this

This reverts commit 7a153ddea067b24da59f6a66c733d79205969501.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@329867 91177308-0d34-0410-b5e6-96231b3b80d8
2018-04-12 00:40:50 +00:00
Jake Ehrlich
6450f7fb01 [llvm-objcopy] Switch over to using TableGen for parsing arguments
Swithces from using the command line library to using TableGen. This will allow
llvm-strip to exist and allow refinements of the command line syntax.

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@329863 91177308-0d34-0410-b5e6-96231b3b80d8
2018-04-11 23:37:03 +00:00
Alexander Shaposhnikov
36cdb40ffa [llvm-objcopy] Implement support for section groups
This diff adds support for SHT_GROUP sections to llvm-objcopy.
Some sections are interrelated and comprise a group.
For example, a definition of an inline function might require, 
in addition to the section containing its instructions, 
a read-only data section containing literals referenced inside the function.
A section of the type SHT_GROUP contains the indices of the group members,
therefore, it needs to be updated whenever the indices change.
Similarly, the fields sh_link, sh_info should be recalculated as well.

[Resubmit r328012 with the proper handling of endianness]

Test plan: make check-all

Differential revision: https://reviews.llvm.org/D43996



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@328143 91177308-0d34-0410-b5e6-96231b3b80d8
2018-03-21 19:53:44 +00:00
Alexander Shaposhnikov
823e852294 [llvm-objcopy] Revert r328012
Temporarily revert r328012 (since it broke down the big-endian bots),
will resubmit an updated version later.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@328024 91177308-0d34-0410-b5e6-96231b3b80d8
2018-03-20 19:46:00 +00:00
Alexander Shaposhnikov
e89563694f [llvm-objcopy] Implement support for section groups
This diff adds support for SHT_GROUP sections to llvm-objcopy.
Some sections are interrelated and comprise a group.
For example, a definition of an inline function might require, 
in addition to the section containing its instructions, 
a read-only data section containing literals referenced inside the function.
A section of the type SHT_GROUP contains the indices of the group members,
therefore, it needs to be updated whenever the indices change.
Similarly, the fields sh_link, sh_info should be recalculated as well.

Test plan: make check-all

Differential revision: https://reviews.llvm.org/D43996


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@328012 91177308-0d34-0410-b5e6-96231b3b80d8
2018-03-20 18:20:42 +00:00
Jake Ehrlich
2bc4193b4d Revert "[llvm-objcopy] Add support for large indexes"
Not all build bots have unzip which I used in a test.

This reverts commit 0b1f26d39ea42dd3716b525fbc8c78d8c7bb4479.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326941 91177308-0d34-0410-b5e6-96231b3b80d8
2018-03-07 20:33:02 +00:00
Jake Ehrlich
96db657c27 [llvm-objcopy] Add support for large indexes
Because of -ffunction-sections (and maybe other use cases I'm not aware of?) it
can occur that we need more than 0xfeff sections but ELF dosn't support that
many sections. To solve this problem SHN_XINDEX exists and with it come a whole
host of changes for section indexes everywhere. This change adds support for
those cases which should allow llvm-objcopy to copy binaries that have an
arbitrary number of sections.

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326940 91177308-0d34-0410-b5e6-96231b3b80d8
2018-03-07 19:59:15 +00:00
Alexander Shaposhnikov
b4ef46d3c7 [llvm-objcopy] Fix typo in setSymTab
This diff fixes the name of the argument of 
setSymTab and makes setSymTab/setStrTab private 
(to make the public interface a bit cleaner).

Test plan: make check-all

Differential revision: https://reviews.llvm.org/D43661


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326005 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-24 00:41:01 +00:00
Alexander Richardson
fb04979cdf [llvm-objcopy] Use the full filename in --add-gnu-debuglink
Summary:
The current implementation was writing the file name without the extension
whereas GNU objcopy writes the full filename. With this change GDB will now
load the .debug file instead of silently ignoring it.

Reviewers: jakehehrlich, jhenderson

Reviewed By: jakehehrlich

Subscribers: llvm-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325528 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-19 19:53:44 +00:00
Jake Ehrlich
2785020fa2 [llvm-objcopy] Fix handling of zero-size segments in llvm-objcopy
Some ELF files produced by lld may have zero-size segment placeholders as shown
below. Since GNU_STACK Offset is 0, the current code makes it the lowest used
offset, and relocates all the segments over the ELF header. The resulting
binary is total garbage.

This change fixes how llvm-objcopy handles PT_PHDR properlly by treating ELF
headers and the program header table as segments to allow the layout algorithm
decide where those should go.

Author: vit9696

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325189 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-14 23:31:33 +00:00
Alexander Shaposhnikov
09d56824f5 [llvm-objcopy] Make modifications in-place if output is not specified
If the output file is not specified make the modifications in-place 
(like binutils objcopy does). In particular, this fixes 
the behavior of Clang -gsplit-dwarf (if Clang is configured to use llvm-objcopy), 
previously it was creating .dwo files, but still leaving *dwo* sections in 
the original binary.

Test plan: make check-all

Differential revision: https://reviews.llvm.org/D42873


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@324783 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-09 23:33:31 +00:00
Jake Ehrlich
e000998952 Reland "[llvm-objcopy] Refactor llvm-objcopy to use reader and writer objects"
Somehow I reverted changes I made in a previous Reland. This change re-relands
unconfusing a varible name with a type name.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@323494 91177308-0d34-0410-b5e6-96231b3b80d8
2018-01-26 02:01:37 +00:00
Jake Ehrlich
1b9f1faf57 Reland "[llvm-objcopy] Refactor llvm-objcopy to use reader and writer objects"
I had more unused varibles. This change removes those to get rid of warnings.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@323493 91177308-0d34-0410-b5e6-96231b3b80d8
2018-01-26 01:48:12 +00:00
Jake Ehrlich
6ea62d72a9 Reland "[llvm-objcopy] Refactor llvm-objcopy to use reader and writer objects"
Added line to output the proper files in the output to binary case.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@323489 91177308-0d34-0410-b5e6-96231b3b80d8
2018-01-26 01:17:35 +00:00