C++

R_X86_64_PC32 Relocation Error Fix

A shared object can be loaded at any address, so code inside it cannot reach a global variable through a fixed PC-relative offset. When an object compiled without -fPIC does exactly that, the linker refuses the link and names the symbol: read the symbol in the message, find the object that accesses it, recompile that object with -fPIC, and link again.

Written November 26, 2026 · Tested with GCC 13.3.0 · 15 min read

By the end of this guide
  • The first error: a global variable reached through a static archive
  • The fix: recompile the object with -fPIC, rebuild the archive, link again
  • The same fix with -fpic
  • A plain object file on the link line, no archive

Written November 26, 2026

Tested with GCC 13.3.0

What this guide is

This error is the linker refusing to put position-dependent code into a shared object. A shared object is loaded at an address chosen at run time, so the distance between an instruction and a global variable is not fixed when the program is built. Code compiled without -fPIC can still record that distance as an R_X86_64_PC32 relocation: a 32-bit PC-relative reference to the symbol. When the linker meets that relocation while making a shared object, it stops, because the reference cannot be resolved correctly once the object is loaded at an arbitrary address.

The message names the symbol, and the symbol is the work list. The object that contains the relocation is the object whose code accesses that symbol: a function that reads or writes a global variable, an array, or a global object. The fix is applied to that object, not to the link command: recompile it with -fPIC, rebuild the static archive if the object reached the link through one, and link again. Adding -fPIC to the final gcc -shared command alone does not repair an object that was already compiled without it.

Three shapes produce the same message. A global variable is accessed from an object pulled out of a static archive. The same access comes from a plain object file named directly on the link line, with no archive in between. And a C++ global object, here a std::string, is accessed from an archive member, so the message names the mangled C++ symbol. Each section below takes one shape, shows the exact message it prints, and gives the fix that belongs to that shape.

Two nearby shapes do not fail, and they are the usual source of confusion when this error appears in a larger build. An object whose functions touch no global data links into a shared object cleanly without -fPIC. And the same non-PIC archive that fails a shared-object link links into an ordinary executable without complaint. Both are shown with real output below, because they narrow the search: the defect is one object, reaching one global symbol, in a shared-object link.

The examples here use GCC 13.3.0 and GNU ld. Each failing link prints its diagnostic to standard error and exits with status 1; each fixed link is followed by a small program that links against the shared object and runs, and its output is shown exactly as printed.

The first error: a global variable reached through a static archive

The smallest shape defines a global variable in g.c and reads it in a function. The file is compiled without -fPIC and put into a static archive:

C
int counter = 7;

int get(void) {
    return counter;
}

readelf -r on the object shows the relocation the compiler recorded: an R_X86_64_PC32 entry against counter in the .rela.text section. The second file, h.c, only calls get():

C
int get(void);

int call_get(void) {
    return get();
}

Linking h.c into a shared object pulls g.o out of the archive to resolve get(), and the link fails on the relocation inside it:

Terminal
$ gcc -c g.c -o g.o
$ readelf -r g.o

Relocation section '.rela.text' at offset 0x170 contains 1 entry:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
00000000000a  000300000002 R_X86_64_PC32     0000000000000000 counter - 4

Relocation section '.rela.eh_frame' at offset 0x188 contains 1 entry:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000000020  000200000002 R_X86_64_PC32     0000000000000000 .text + 0
$ ar rcs libg.a g.o
$ gcc -shared -o libh.so h.c -L. -lg
/usr/bin/ld: ./libg.a(g.o): warning: relocation against `counter' in read-only section `.text'
/usr/bin/ld: ./libg.a(g.o): relocation R_X86_64_PC32 against symbol `counter' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status

The warning line comes first and names the same symbol; the error line is the refusal. The linker names ./libg.a(g.o), the archive member that carries the relocation, and counter, the symbol it points at. Nothing in h.c is at fault: h.c never touches counter. The object to fix is g.o, the one whose function reads the global.

Recompile g.c with -fPIC, put the new object into the archive so the archive no longer holds the old one, and repeat the same link. A small consumer program then links against the shared object and runs:

C
#include <stdio.h>

int call_get(void);

int main(void) {
    printf("%d\n", call_get());
    return 0;
}
Terminal
$ gcc -fPIC -c g.c -o g.o
$ ar rcs libg.a g.o
$ gcc -shared -o libh.so h.c -L. -lg
$ gcc main.c -o main -L. -lh -Wl,-rpath,.
$ ./main
7

The link prints nothing, and the consumer prints 7, the value of counter, reached through call_get() in the shared object and get() from the archive. Two details matter in a real build. The archive must be rebuilt after the recompile: ar rcs replaces the member, and skipping that step links the old non-PIC object again and reproduces the same error. And every object that reaches a global needs the same treatment: fixing one member clears its symbol from the message, and the next non-PIC member, if there is one, is named in its place.

The same fix with -fpic

On x86-64, the smaller -fpic form also fixes this link. Position-independent code accesses constant addresses through a global offset table, and -fPIC differs from -fpic by avoiding any limit on the size of that table — a difference the GCC manual places on AArch64, m68k, PowerPC and SPARC, while stating that the x86 has no such limit. Recompiling the same file with -fpic and relinking succeeds:

Command
$ gcc -fpic -c g.c -o g.o
$ ar rcs libg.a g.o
$ gcc -shared -o libh.so h.c -L. -lg

That is an x86-64 result, not a general one. On a target with a GOT size limit, a large library can outgrow -fpic and the linker reports that -fpic does not work; the manual's direction in that case is to recompile with -fPIC. Build systems that must work across targets use -fPIC for shared-object code for that reason.

The archive is not part of the cause. Here d.c defines a global array and a function that reads it, and the object file is named directly on the link line next to e.c, which only calls the function:

C
int table[3] = {10, 20, 30};

int pick(int i) {
    return table[i];
}
C
int pick(int i);

int call_pick(void) {
    return pick(1);
}
Terminal
$ gcc -c d.c -o d.o
$ gcc -shared -o libe.so e.c d.o
/usr/bin/ld: d.o: warning: relocation against `table' in read-only section `.text'
/usr/bin/ld: d.o: relocation R_X86_64_PC32 against symbol `table' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status

The wording is the same; only the named input changes. The linker names d.o itself instead of an archive member, and the symbol is table. When a build's message names a plain .o file, that file is the one to recompile — there is no archive to rebuild, and no other input to suspect first.

The fix for the direct object

Recompile d.c with -fPIC and repeat the link. The consumer calls pick(1) through the shared object and prints the second element of the array:

C
#include <stdio.h>

int pick(int i);

int main(void) {
    printf("%d\n", pick(1));
    return 0;
}
Terminal
$ gcc -fPIC -c d.c -o d.o
$ gcc -shared -o libe.so e.c d.o
$ gcc main_b.c -o main_b -L. -le -Wl,-rpath,.
$ ./main_b
20

The consumer prints 20, which is table[1]: the array access that produced the relocation now runs inside the shared object.

The C++ shape: a global std::string in an archive

C++ produces the same relocation for the same reason. Here s.cpp defines a global std::string and a function that returns a reference to it, and the object goes into an archive. u.cpp only calls that function:

C++
#include <string>

std::string label = "alpha";

const std::string& get_label() {
    return label;
}
C++
#include <string>

const std::string& get_label();

const std::string& call_label() {
    return get_label();
}
Terminal
$ g++ -c s.cpp -o s.o
$ ar rcs libs.a s.o
$ g++ -shared -o libu.so u.cpp -L. -ls
/usr/bin/ld: ./libs.a(s.o): warning: relocation against `_Z5labelB5cxx11' in read-only section `.text'
/usr/bin/ld: ./libs.a(s.o): relocation R_X86_64_PC32 against symbol `_Z5labelB5cxx11' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status

The symbol in the message is _Z5labelB5cxx11, the mangled name of the label variable, including its C++11 ABI tag. A mangled name in this error is read the same way as a plain one: it is the global being accessed, and s.o inside libs.a is the object that accesses it. Any global with a non-trivial type — a std::string, a global object with a constructor, a lookup table — is reached the same way by code compiled without -fPIC, so C++ archives built for static linking hit this error as soon as they are reused in a shared object.

The fix for the C++ archive

Recompile s.cpp with -fPIC, rebuild the archive, and link again. The consumer prints the string through the shared object:

C++
#include <iostream>
#include <string>

const std::string& get_label();

int main() {
    std::cout << get_label() << '\n';
    return 0;
}
Terminal
$ g++ -fPIC -c s.cpp -o s.o
$ ar rcs libs.a s.o
$ g++ -shared -o libu.so u.cpp -L. -ls
$ g++ main_c.cpp -o main_c -L. -lu -Wl,-rpath,.
$ ./main_c
alpha

The consumer prints alpha, the value of the global label, read through get_label() in the shared object.

Not every non-PIC object fails. f.c defines a function that works only on its parameters and a local result; no global variable, array, or string literal table is reached. Archived and linked into a shared object without -fPIC, it links cleanly, and the consumer runs:

C
int add(int a, int b) {
    return a + b;
}
C
int add(int a, int b);

int call_add(void) {
    return add(2, 3);
}
C
#include <stdio.h>

int call_add(void);

int main(void) {
    printf("%d\n", call_add());
    return 0;
}
Terminal
$ gcc -c f.c -o f.o
$ ar rcs libf.a f.o
$ gcc -shared -o libfc.so fc.c -L. -lf
$ gcc main_f.c -o main_f -L. -lfc -Wl,-rpath,.
$ ./main_f
5

The consumer prints 5. There is no R_X86_64_PC32 relocation against a data symbol in f.o, because add() never reaches outside its own frame. This is why the error in a large build names some objects and not others, and why "the library was built without PIC" is too broad a diagnosis: the failing unit is the individual object that accesses global data. A function that calls another function can still be fine; a function that reads one global variable that other files can link to is not. A variable marked static in its own file is the exception: it has no symbol of its own, the access is recorded against the section instead, and that reference links into a shared object without -fPIC.

The output kind matters as much as the object. Take the original non-PIC g.o in its archive — the exact input that failed the shared-object link — and link it into an ordinary executable instead:

Terminal
$ gcc -c g.c -o g.o
$ ar rcs libg.a g.o
$ gcc -o prog h.c main.c -L. -lg
$ ./prog
7

The link succeeds and the program prints 7. An ordinary executable is not required to work at an arbitrary load address in the way a shared object is, so the linker accepts the PC-relative reference there. The practical consequence: a static archive can be correct for every executable that uses it and still be unusable inside a shared object. When a project starts shipping a shared library, its archives need a PIC build of their own rather than a reuse of the executable build, which is why many build systems compile each source twice, once per output kind.

The exact rule: a shared object cannot take a PC-relative reference to a global

-shared asks the linker to create a shared library. A shared library is loaded at an address chosen by the dynamic loader, so no fixed offset between code and data inside it can be assumed at build time. Position-independent code handles this by accessing constant addresses through the global offset table, whose entries the dynamic loader resolves when the program starts. -fpic generates that code with a possible limit on the table size; -fPIC generates it avoiding any limit on the size of the table.

Code compiled without either flag may reach a global variable through an R_X86_64_PC32 relocation, a 32-bit PC-relative reference to the symbol, as the readelf -r output in the first section shows. That relocation is usable in an ordinary executable link and unusable in a shared object. When the linker finds one while making a shared object, it refuses with the message this guide reproduces, names the symbol and the input file or archive member that carries the relocation, adds final link failed: bad value, and the compile driver reports that ld returned exit status 1. The only repair is at the source of the relocation: recompile the object that accesses the symbol with -fPIC (or -fpic where its limits hold) so the access goes through the GOT instead, then rebuild any archive that holds the object and link again.

The fixes, ranked

  • First, read the symbol and the input named in the message. The input — a plain object file or an archive member in parentheses — is the object to recompile, and the symbol is the global its code accesses.
  • Second, recompile that object's source with -fPIC and repeat the link. If the message returns naming a different symbol or member, repeat for that object; a build can hold several non-PIC objects, and the linker reports them one link at a time.
  • Third, if the object reached the link through a static archive, rebuild the archive from the recompiled object before relinking. Relinking against the old archive reproduces the identical error.
  • Fourth, give the project a separate PIC build of any archive that is used by both executables and shared objects. The executable build is not wrong; it is built for a different output kind.
  • Fifth, use -fPIC rather than -fpic for shared-object code unless the target is known: the two agree on x86-64, and -fPIC avoids the GOT size limit that applies on some other targets.

Which fix for which cause

Cause Message Fix
Global variable accessed from an object in a static archive relocation R_X86_64_PC32 against symbol 'counter', input named as ./libg.a(g.o) Recompile that object's source with -fPIC, rebuild the archive, link again
Global array accessed from a plain object file on the link line Same message, symbol table, input named as d.o Recompile that object with -fPIC, link again; no archive to rebuild
C++ global object accessed from an archive member Same message with the mangled symbol, here _Z5labelB5cxx11 for a global std::string Recompile that object's source with -fPIC, rebuild the archive, link again
Archive rebuilt from the old object, or the recompile skipped Identical message after an apparent fix Check that the archive member is the recompiled object; ar replaces the member only when the archive step is rerun
Function-only object with no global data access No error; the shared-object link succeeds without -fPIC None needed; the object carries no PC-relative reference to a data symbol
Same non-PIC archive linked into an ordinary executable No error; the executable links and runs None needed for the executable; build a PIC copy of the archive for shared-object use
## Checklist
  • Read the input named in the message first: lib.a(file.o) means the member inside the archive; a bare file.o means that object directly.
  • Read the symbol next: it is the global being accessed — a variable, an array, or, under a mangled name, a C++ global object.
  • Recompile the source of the named object with -fPIC; changing only the final link command does not repair an already-compiled object.
  • Rebuild every archive that contains a recompiled object before relinking.
  • Expect the message to return for the next non-PIC object in a multi-object build; work through them in the order the linker names them.
  • Do not chase objects the linker does not name: function-only objects without global data access link into shared objects without -fPIC.
  • Keep executable and shared-object builds of the same archive separate; an archive can be right for one output kind and unusable for the other.
  • Prefer -fPIC for shared-object code that must build on more than x86-64; -fpic carries a GOT size limit on some targets.

Sources

  • GCC, Code Gen Options — for -fpic generating position-independent code suitable for use in a shared library, for such code accessing all constant addresses through a global offset table whose entries the dynamic loader resolves when the program starts, for the GOT size maximums that apply to -fpic on some targets and the direction to recompile with -fPIC when they are exceeded, for the x86 having no such limit, and for -fPIC emitting position-independent code suitable for dynamic linking while avoiding any limit on the size of the global offset table.
  • GNU ld, Options — for -shared creating a shared library.

Keep Exploring