tags:

views:

766

answers:

3

I'm trying to compile shared library on solaris 2.7 using gcc 3.4.6 and which is linking to a statically linked c .a and .o files. Please note that it is using Sun ld from path "/usr/ccs/bin/ld"

At linking time i got a long list of symbols and following error

ld: fatal: relocations remain against allocatable but non-writable sections

collect2: ld returned 1 exit status

Then i tried to build it passing -z textoff option to ld. but i'm getting follwing error

ld: fatal: option -ztextoff and -ztext are incompatible

ld: fatal: Flags processing errors

Is there any other way where i don't need to recompile gcc and still modify the options getting passed to ld.

A: 

Run the ld executable from the command line (not via gcc) - you can then pass it whatever parameters you want. I don't think that will solve your underlying problems though - you might want to post a question about them.

anon
@Neil: Thanks , I'll try that. might be little difficult as project has good no of files. So is there a way i can modify options passed by gcc so that i need not to call ld manually. ( of-course without recompiling it )Sorry but i didn't get your point "underlying problem"
nurxb01
This is a terrible idea, and almost never the correct thing to do.
Employed Russian
A: 

Are you using make or some other build system to invoke the compiler?

If you change the options in the build system to specifically use the linker during the link phase rather than using the compiler.

Step 1: Find flags passed by gcc

Add the -v flag. It makes gcc verbose.

CXXFLAGS += -v

Step 2: Modify the link stage to explicitly use the tool that gcc was invoking.

Martin York
We are using gnu make.Do you mean in the Makefile i should us ld and pass object files directly with appropriate linker options?Can be done but i have to prepare that option list required to be passed to ld very carefully.Is there an easy way by which i can find out what all options are being passed to ld by gcc.So that when i call ld manually, i can pass all of them removing -ztext option.I try using -v option but that prints arguments passed to collect2 utility.As i mentioned in comments to my question, modifying the specs file seems to be easier in this particular case.
nurxb01
@Martin as mentioned in my comment I did the same passed -v flag to gcc , and it gives me arguments passed to collect2 utility and not to the underlying sun linker.What's your opinion regarding using the specialized specs file to achieve this?
nurxb01
I would be more inclined to use the coolect2 utility rather than building your own specs files.
Martin York
But at this point I am stuck.
Martin York
Invoking underlying linker directly is a terrible idea. It leads to a link line which will likely be incorrect for the next version if GCC or the new version of the OS. Just don't do it(TM).
Employed Russian
+1  A: 

The errors are the result of linking position-dependent code into a shared library. Such code will result in the library not being shareable, and thus wasting RAM.

If you can rebuild all the objects you are trying to link into the shared library, the simplest (and most correct) solution is to rebuild all of them with -fPIC flag.

However, sometimes you really must link non-PIC object code which you can't rebuild into a shared library, and therefore you need to get rid of the -ztext option. To do that, add -mimpure-text option to your link line.

Employed Russian
Thanks!! Yeah I'm in one such situation, and i have to use this statically linked code within a shared object.Yes "-mimpure-text" option worked for me.I checked and found out that even in the spec file , i have modified this parameter, but using this -mimpure-text is the right way.Thanks again, it solves one of my long pending issue.
nurxb01