C++
R_X86_64_32 Relocation Error Fix
A shared object can be loaded at any address, so code inside it cannot hold a string literal's address as a fixed 32-bit value. When an object compiled without -fPIC does exactly that, the linker refuses the link and names the section: recompile the object that references .rodata with -fPIC, and link again.
Written November 26, 2026 · Tested with GCC 13.3.0 · 12 min read
- The error: a string literal reached from a -fno-pic object
- The fix: recompile the object with -fPIC, link again
- The same fix with -fpic
- A probe: what -fpie does to the same object
Written November 26, 2026
Tested with GCC 13.3.0
What this guide is
This error is the linker refusing to put an absolute address into a shared object. A shared object is loaded at an address chosen at run time, so the address of a string literal inside it is not known when the program is built. Code compiled without -fPIC can still record that address as an R_X86_64_32 relocation: a fixed 32-bit reference to the .rodata section, where string literals live. When the linker meets that relocation while making a shared object, it stops, because the value it would have to write is the literal's address at a load address nobody has chosen yet.
The message names the section, and the section is the work list. The object that contains the relocation is the object whose code reaches into .rodata: a function that returns or prints a string literal, reads a constant table, or touches other read-only data. The fix is applied to that object, not to the link command: recompile it with -fPIC, and link again. Adding -fPIC to the final g++ -shared command alone does not repair an object that was already compiled without it.
This guide is about the absolute form of the refusal. Its neighbor, the R_X86_64_PC32 error, is a PC-relative reference to a named global symbol; this one is an absolute reference to a section, .rodata, and the symbol column in the relocation entry names the section itself rather than a variable. The two errors share a cause, code built without position independence reaching data it cannot address from an arbitrary load address, and they share a fix, but they are diagnosed differently: here there is no variable name to search for, so the hunt starts from the object file and its .rodata reference.
Two nearby shapes behave differently, and both are shown with real output below. An object whose only function returns an integer, reaching no read-only data at all, links into a shared object cleanly without -fPIC. And the same string-literal object that fails a shared-object link links into a non-PIE executable, while a default PIE executable link refuses it again with a differently worded message. Those shapes narrow the search in a larger build: the defect is one object, one absolute reference to .rodata, in a position-independent output.
The examples here use GCC 13.3.0 and GNU ld, with C++20 sources. Each failing link prints its diagnostic to standard error and exits with status 1; the main 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 error: a string literal reached from a -fno-pic object
The smallest shape is one function that returns a string literal. The literal itself is stored in .rodata; returning it means producing its address:
const char* get_msg() {
return "hello from the shared library";
}
Compiled with -fno-pic, the object records how that address is produced, and readelf -r shows it: an R_X86_64_32 entry against .rodata in the .rela.text section. The addend is 0, the offset of the literal at the start of the section:
$ g++ -std=c++20 -fno-pic -c msg.cpp -o msg.o
$ readelf -r msg.o
Relocation section '.rela.text' at offset 0x188 contains 1 entry:
Offset Info Type Sym. Value Sym. Name + Addend
000000000009 00030000000a R_X86_64_32 0000000000000000 .rodata + 0
Relocation section '.rela.eh_frame' at offset 0x1a0 contains 1 entry:
Offset Info Type Sym. Value Sym. Name + Addend
000000000020 000200000002 R_X86_64_PC32 0000000000000000 .text + 0
Linking that object into a shared object fails on the relocation inside it:
$ g++ -shared -o libmsg.so msg.o
/usr/bin/ld: msg.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: failed to set dynamic section sizes: bad value
collect2: error: ld returned 1 exit status
Read the two linker lines together. The first names the input, msg.o, the relocation type, R_X86_64_32, and the section it points at, .rodata, and it ends with the repair: recompile with -fPIC. The second line, failed to set dynamic section sizes: bad value, is the linker's closing report for the same refusal, not a second defect to chase. collect2 then reports that ld returned exit status 1. Nothing else in the shape is at fault: the source is correct, and the same object is used unchanged in the executable sections below.
The fix: recompile the object with -fPIC, link again
Recompile msg.cpp with -fPIC so the object no longer carries the absolute reference, and repeat the same link. A small consumer program then links against the shared object and runs:
#include <iostream>
const char* get_msg();
int main() {
std::cout << get_msg() << '\n';
return 0;
}
$ g++ -std=c++20 -fPIC -c msg.cpp -o msg.o
$ g++ -shared -o libmsg.so msg.o
$ g++ -std=c++20 main.cpp -o main -L. -lmsg -Wl,-rpath,.
$ ./main
hello from the shared library
The link prints nothing, and the consumer prints hello from the shared library, the literal the function returns, now reached through position-independent code inside the shared object. Two details matter in a real build. The recompile is the fix, not the link line: an object that already holds an R_X86_64_32 reference to .rodata carries it into every link until its source is recompiled. And every object that reaches read-only data needs the same treatment: fixing one object clears its reference from the message, and the next non-PIC object, 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:
$ g++ -std=c++20 -fpic -c msg.cpp -o msg.o
$ g++ -shared -o libmsg.so msg.o
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.
A probe: what -fpie does to the same object
Compiling the same source with -fpie also removes the absolute reference. The readelf output changes in the same place: the .rela.text entry is now an R_X86_64_PC32 reference to .rodata, and the shared link that refused the -fno-pic object accepts this one:
$ g++ -std=c++20 -fpie -c msg.cpp -o msg.o
$ readelf -r msg.o
Relocation section '.rela.text' at offset 0x188 contains 1 entry:
Offset Info Type Sym. Value Sym. Name + Addend
00000000000b 000300000002 R_X86_64_PC32 0000000000000000 .rodata - 4
Relocation section '.rela.eh_frame' at offset 0x1a0 contains 1 entry:
Offset Info Type Sym. Value Sym. Name + Addend
000000000020 000200000002 R_X86_64_PC32 0000000000000000 .text + 0
$ g++ -shared -o libmsg.so msg.o
That result is a probe, not the fix to adopt. The GCC manual describes the -fpie family as generating position-independent code that can only be linked into executables, while -fPIC is the form documented as suitable for dynamic linking. A build that needs a shared object should be repaired with -fPIC, which is also what the linker's own message names; the -fpie result is useful only because it confirms the diagnosis, that the absolute relocation, and nothing else in the object, was the blocker.
A neighbor that links without -fPIC: an integer-only object
Not every non-PIC object fails. num.cpp defines a function that returns an integer constant; no string literal, no constant table, no read-only data is reached. Its readelf output has no .rela.text entry at all, only the .eh_frame entry every object here carries. Compiled without -fPIC and linked into a shared object, it links cleanly, and the consumer runs:
int get_num() {
return 42;
}
#include <iostream>
int get_num();
int main() {
std::cout << get_num() << '\n';
return 0;
}
$ g++ -std=c++20 -fno-pic -c num.cpp -o num.o
$ readelf -r num.o
Relocation section '.rela.eh_frame' at offset 0x150 contains 1 entry:
Offset Info Type Sym. Value Sym. Name + Addend
000000000020 000200000002 R_X86_64_PC32 0000000000000000 .text + 0
$ g++ -shared -o libnum.so num.o
$ g++ -std=c++20 main_num.cpp -o main_num -L. -lnum -Wl,-rpath,.
$ ./main_num
42
The consumer prints 42. 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 reaches .rodata. A function that works only on its parameters and constants has no absolute reference to refuse; the first string literal or constant table in a file is what creates one.
The same object in an executable: -no-pie links, a default PIE link refuses it
The output kind matters as much as the object. Take the original non-PIC msg.o — the exact input that failed the shared-object link — and link it into an executable with -no-pie, so the executable is built for a fixed load address:
$ g++ -std=c++20 -fno-pic -c msg.cpp -o msg.o
$ g++ -std=c++20 -no-pie main.cpp msg.o -o main_nopie
$ ./main_nopie
hello from the shared library
The link succeeds and the program prints hello from the shared library. A non-PIE executable is not required to work at an arbitrary load address, so the linker can write the literal's fixed address and be done. The practical consequence: an object can be correct for every non-PIE executable that uses it and still be unusable inside a shared object, which is why a project that starts shipping a shared library needs a PIC build of its objects rather than a reuse of the executable build.
The same object does not survive a default executable link on a toolchain that builds PIE executables by default, as this one does. The refusal returns with different wording, naming a PIE object instead of a shared object, and -fPIE instead of -fPIC:
$ g++ -std=c++20 main.cpp msg.o -o main_direct
/usr/bin/ld: msg.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: failed to set dynamic section sizes: bad value
collect2: error: ld returned 1 exit status
So "it links into our executable" is only half a test. It clears the object when the executable is non-PIE, and it proves nothing when the executable link was never tried, because a PIE executable imposes the same position-independence requirement as a shared object, with its own version of the message. When this error appears, read which output kind the message names — shared object or PIE object — because that word tells you which link in the build actually failed.
The exact rule: a shared object cannot take an absolute 32-bit reference to .rodata
-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 address for anything 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 with -fno-pic, and equally with -fno-pie, may reach a string literal through an R_X86_64_32 relocation, a fixed 32-bit reference to the .rodata section, as the readelf -r output in the first section shows. That relocation is usable in a non-PIE executable link and unusable in a shared object and in a PIE executable. When the linker finds one while making a shared object, it refuses with the message this guide reproduces, names the input file and the section, adds failed to set dynamic section sizes: 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 references .rodata with -fPIC so the reference is position-relative instead, then link again.
The fixes, ranked
- First, read the input and the section named in the message. The input file is the object to recompile, and
.rodatasays its code reaches read-only data — a string literal, a constant table, or similar contents. - Second, recompile that object's source with
-fPICand repeat the link. If the message returns naming a different input, repeat for that object; a build can hold several non-PIC objects, and the linker reports them one link at a time. - Third, confirm with
readelf -rwhen the named input is not obvious: anR_X86_64_32entry against.rodatain.rela.textis the relocation to remove, and after the recompile that entry is gone, replaced by a PC-relative reference. - Fourth, give the project a separate PIC build of any object 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
-fPICrather than-fpicfor shared-object code unless the target is known: the two agree on x86-64, and-fPICavoids the GOT size limit that applies on some other targets. Do not substitute-fpieon the strength of a single successful probe; it is documented for executables, not for shared objects.
Which fix for which cause
| Cause | Message | Fix |
|---|---|---|
String literal returned or used from an object compiled with -fno-pic |
relocation R_X86_64_32 against '.rodata' can not be used when making a shared object, input named as msg.o |
Recompile that object's source with -fPIC, link again |
Same shape compiled with -fno-pie |
Identical message and identical R_X86_64_32 entry against .rodata |
Same fix; -fno-pie produces the same absolute reference in this shape |
| Object recompiled but the link still fails with the same message | Identical message after an apparent fix | Check that the link is using the recompiled object, not a stale copy of the old one from another directory or build tree |
| Integer-only object with no read-only data access | No error; the shared-object link succeeds without -fPIC |
None needed; the object carries no absolute reference to .rodata |
| Same non-PIC object linked into a non-PIE executable | No error; the executable links and runs | None needed for the executable; build a PIC copy of the object for shared-object use |
| Same non-PIC object linked into a default PIE executable | relocation R_X86_64_32 against '.rodata' can not be used when making a PIE object; recompile with -fPIE, with the same closing lines |
Recompile with -fPIE for the executable, or -fPIC if the object is also used in a shared object |
| ## Checklist |
- Read the input named in the message first: that file is the object to recompile, and no other input is suspect until it is fixed.
- Read the section next:
.rodatameans the object's code reaches read-only data, most often a string literal; there is no variable name in this message to search for. - Recompile the source of the named object with
-fPIC; changing only the final link command does not repair an already-compiled object. - Verify the repair in the object when in doubt:
readelf -rshould no longer show anR_X86_64_32entry against.rodata. - 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: an object with no
.rodatareference links into shared objects without-fPIC. - Keep executable and shared-object builds separate, and remember which executable kind a passing test used: a
-no-pielink clears an object that a PIE link and a shared link both refuse. - Prefer
-fPICfor shared-object code that must build on more than x86-64;-fpiccarries a GOT size limit on some targets.
Related guides on this site
- R_X86_64_PC32 Relocation Error Fix, the companion relocation refusal, for a PC-relative reference to a named global symbol rather than an absolute reference to
.rodata. - DSO Missing From Command Line Fix, another GNU ld refusal with its own repair, for a library the linker found through a dependency but would not use for the program's own call.
- Fix ld Cannot Find Library Search Path, the companion linker error, for a library the linker cannot find at all rather than an object it refuses.
Sources
- GCC, Code Gen Options — for
-fpicgenerating 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-fpicon some targets and the direction to recompile with-fPICwhen they are exceeded, for the x86 having no such limit, for-fPICemitting position-independent code suitable for dynamic linking while avoiding any limit on the size of the global offset table, and for the-fpiefamily generating position-independent code that can only be linked into executables. - GNU ld, Options — for
-sharedcreating a shared library.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.