1. 程式人生 > 實用技巧 >can not be used when making a PIE object; recompile with -fPIC

can not be used when making a PIE object; recompile with -fPIC

  問題:

roroot@data-server /m/o/tengine [2]# ll
總用量 420K
-rw-rw-r--  1 root root  889 9月   5 2019 AUTHORS.te
drwxrwxr-x  6 root root 4.0K 9月   5 2019 auto/
-rw-rw-r--  1 root root 292K 9月   5 2019 CHANGES
-rw-rw-r--  1 root root  26K 9月   5 2019 CHANGES.cn
-rw-rw-r--  1 root root  32K 9月   5 2019 CHANGES.te
drwxrwxr-x  2 root root 4.0K 9月   5 2019 conf/
-rwxrwxr-x  1 root root 2.5K 9月   5 2019 configure*
drwxrwxr-x  4 root root 4.0K 9月   5 2019 contrib/
drwxrwxr-x  4 root root 4.0K 9月   5 2019 docs/
drwxrwxr-x  2 root root 4.0K 9月   5 2019 html/
-rw-rw-r--  1 root root 1.7K 9月   5 2019 LICENSE
-rw-------  1 root root  380 9月  14 02:40 Makefile
drwxrwxr-x  2 root root 4.0K 9月   5 2019 man/
drwxrwxr-x 26 root root 4.0K 9月   5 2019 modules/
drwx------  4 root root 4.0K 9月  14 11:55 objs/
drwxrwxr-x  3 root root 4.0K 9月   5 2019 packages/
-rw-rw-r--  1 root root 3.4K 9月   5 2019 README.markdown
drwxrwxr-x 10 root root 4.0K 9月   5 2019 src/
drwxrwxr-x  4 root root 4.0K 9月   5 2019 tests/
-rw-rw-r--  1 root root   43 9月   5 2019 THANKS.te
root@data-server /m/o/tengine# ./configure --prefix=/usr/local/nginx --with-pcre --with-stream  --with-http_perl_module \
                               --with-http_mp4_module \
                               --with-http_flv_module \
                               --with-http_gunzip_module \
                               --with-http_ssl_module \
                               --with-http_realip_module \
                               --with-file-aio \
                               --group=nginx \
                               --user=USER \
                               --conf-path=/etc/nginx/nginx.conf \
                               --sbin-path=/usr/local/sbin/nginx  && make && make install
...
/usr/bin/ld: objs/src/stream/ngx_stream_map_module.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: objs/src/stream/ngx_stream_split_clients_module.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: objs/src/stream/ngx_stream_return_module.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: objs/src/stream/ngx_stream_upstream_hash_module.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: objs/src/stream/ngx_stream_upstream_least_conn_module.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: objs/src/stream/ngx_stream_upstream_random_module.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: objs/src/stream/ngx_stream_upstream_zone_module.o: relocation R_X86_64_32 against symbol `ngx_stream_upstream_module' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: 最後的鏈結失敗: 輸出不可表示的節
collect2: 錯誤:ld 返回 1
make[1]: *** [objs/Makefile:290:objs/nginx] 錯誤 1
make[1]: 離開目錄"/mdata/opt/tengine"
make: *** [Makefile:8:build] 錯誤 2

在編譯nginx的時候遇見上面的錯誤,各種折騰,都沒有解決,最終終於按照同事的思路,找到解決方案了:

  解決辦法:

修改編譯完的檔案:objs/Makefile

在所有包含CFLAGS引數的後面新增 -fPIC,最終改完結果如下:

1 root@data-server /m/o/tengine# head objs/Makefile | grep CFLAGS
2 CFLAGS =  -fPIC -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g
3 NGX_PERL_CFLAGS = -fPIC -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -D_REENTRANT -D_GNU_SOURCE -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2
-Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fwrapv -fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
-I/usr/lib64/perl5/CORE 4 NGX_PM_CFLAGS = -fPIC -D_REENTRANT -D_GNU_SOURCE -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fwrapv -fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/lib64/perl5/CORE
View Code