Tôi muốn xóa tất cả các biểu tượng không sử dụng khỏi nhị phân C++ đã biên dịch của tôi. Tôi thấy điều này, trong đó đưa ra một cái nhìn tổng quan sử dụng gcc, đó là toolchain Tôi đang sử dụng: How to remove unused C/C++ symbols with GCC and ld?tùy chọn trình liên kết để bỏ qua các phụ thuộc không sử dụng
Tuy nhiên, trên hệ thống của tôi, tùy chọn liên kết (-Wl,--gc-sections
) bị từ chối:
$ gcc -fdata-sections -ffunction-sections a.c -o a.o -Wl,--gc-sections
ld: fatal: unrecognized option '--'
ld: fatal: use the -z help option for usage information
collect2: error: ld returned 1 exit status
Tôi chạy trên illumos, một nhánh tương đối gần đây của Solaris, với GCC 4.7. Bất kỳ ai biết tùy chọn liên kết chính xác để sử dụng ở đây là gì?
Edit: tìm kiếm các trang man quay chặt chẽ hơn lên "-zignore":
-z ignore | record
Ignores, or records, dynamic dependencies that are not
referenced as part of the link-edit. Ignores, or
records, unreferenced ELF sections from the relocatable
objects that are read as part of the link-edit. By
default, -z record is in effect.
If an ELF section is ignored, the section is eliminated
from the output file being generated. A section is
ignored when three conditions are true. The eliminated
section must contribute to an allocatable segment. The
eliminated section must provide no global symbols. No
other section from any object that contributes to the
link-edit, must reference an eliminated section.
Tuy nhiên trình tự sau đây vẫn đặt FUNCTION_SHOULD_BE_REMOVED
trong phần ELF .text.FUNCTION
:
$ cat a.c
int main() {
return 0;
}
$ cat b.c
int FUNCTION_SHOULD_BE_REMOVED() {
return 0;
}
$ gcc -fdata-sections -ffunction-sections -c a.c -Wl,-zignore
$ gcc -fdata-sections -ffunction-sections -c b.c -Wl,-zignore
$ gcc -fdata-sections -ffunction-sections a.o b.o -Wl,-zignore
$ elfdump -s a.out # I removed a lot of output for brevity
Symbol Table Section: .dynsym
[2] 0x08050e72 0x0000000a FUNC GLOB D 1 .text.FUNCTION FUNCTION_SHOULD_BE_REMOVED
Symbol Table Section: .symtab
[71] 0x08050e72 0x0000000a FUNC GLOB D 0 .text.FUNCTION FUNCTION_SHOULD_BE_REMOVED
Vì các trang của người đàn ông nói "không có biểu tượng toàn cầu", tôi đã thử làm cho hàm "tĩnh" và có cùng kết quả cuối cùng.
ld phiên bản 2.23.52.0.1 (2013, CentOS 7.0) does't hỗ trợ '-z ignore' không may. –