LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-04-2018, 01:02 PM   #1
beartham
LQ Newbie
 
Registered: Mar 2017
Location: Coupeville, WA
Distribution: Ubuntu 14.04, 16.04, 18.04
Posts: 1

Rep: Reputation: Disabled
Unhappy Linker error building *.so library with *.c program compiled with -fPIC


I am getting a linker error building a shared-object library with a c utility on x86_64 in Ubuntu 16.04. Here is the remake trace of the error:

##>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ar -x libdfcs.a
##<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
##>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
gcc -shared -fPIC *.o -o libdfcs.so
##<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/usr/bin/ld: fcsub.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
fcsub.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
makefile:137: *** [libdfcs.so] Error 1





Also, here is c-code utility causing the issue:

#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>

/* ******* Forward Declarations *************** */
void dbugger(int level, ...);
void sigusr ( int );

/* ********* Pipe globals and functions ********** */
void open_pipe ( char*, int );
int verify_pipe ( char* );
int read_pipe ( char* );
void close_pipe ( void );
char pipe_name[40];
int Pipe_Message_Length = 6;
int Pipe_Handle;
#define FIFO_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)


/* **************** GLOBAL VARIABLES ************************** */
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef GOOD
#define GOOD 0
#endif
#ifndef BAD
#define BAD -1
#endif

#ifndef _DBUGGER_
#define _DBUGGER_$run
#define MANDATORY 0
#define CRITICAL 1
#define EMERGENCY 2
#define SERIOUS 3
#define URGENT 4
#define IMPORTANT 5
#define PRIORITY 6
#define INFORMATION 7
#define LOW_LEVEL 8
#define TRIVIAL 9
#endif

int Wait_Flag = TRUE;
int Hold_Flag = TRUE;
int Alarm_Flag = FALSE;
unsigned int Wait_Time = 100000; // micorseconds
unsigned int Sleep_Time = 500000; // .5 second

/* ****** Required globals for dbugger() ****** */
int DEBUG_LEVEL = 9; /* 0 - 9, nine catches all dbug msgs */
long DEBUG_MAX = 100000L;
char DEBUG_FILE[40];
char PROGRAM_NAME[16];
char *program_name = PROGRAM_NAME;
int PPID; // Parent Pid

//==============================================================
// PEEKING AND POKING
// JMT changed to remove instrumentation 12/29/17
// fcsub.o removed also

/* ------------------ 6464 Architecture
void fcpeek_(out,loc)
__float128 *out;
int64_t *loc;
{
__float128 *d;
d = (__float128 *)*loc;
*out = *d;
}

void fcpoke_(val,loc)
__float128 *val;
int64_t *loc;
{
__float128 *d;
d = (__float128 *)*loc;
*d = *val;
// printf("---in fcpoke_ %16X %16.7G at %8X\n",*d,*d,loc);

}

void fcpeek1_(out,loc)
int64_t *out,*loc;
{
int64_t *d;
d = (int64_t *)*loc;
*out = *d;
}

void fcpeek2_(out,loc)
int64_t *out,*loc;
{
int64_t *d;
d = (int64_t *)*loc;
*out = *(d+1);
}

void fcpoke1_(val,loc)
int64_t *val,*loc;
{
int64_t *d;
d = (int64_t *)*loc;
*d = *val;
}

void fcpoke2_(val,loc)
int64_t *val,*loc;
{/opt/MC7.1/MCperl
int64_t *d;
d = (int64_t *)*loc;
*(d+1) = *val;
}

----------------------------------------*/

/* Old 32-bit test
void fcpoke_(val,loc)
unsigned long *val[2];
unsigned long *loc;
{
unsigned long *d;
d = (unsigned long *)*loc;
val[0] = *d;
val[1] = *d+4;
}
*/


//--------------------- 6432 Architecture

void fcpeek_(out,loc)
double *out;
int *loc;
{
double *d;
d = (double *)*loc;
*out = *d;
// printf("---in fcpeek_ [%16X,%16.7G] at %16X\n",*d,*d,loc);
}

void fcpoke_(val,loc)
double *val;
int *loc;
{
double *d;
d = (double *)*loc;
*d = *val;
// printf("---in fcpoke_ [%16X,%16.7G] at %16X\n",*d,*d,loc);
}

void fcpeek1_(out,loc)
int *out;
int *loc;
{
int *d;
d = (unsigned int *)*loc;
*out = *d;
}

void fcpeek2_(out,loc)
int *out;
int *loc;
{
int *d;
d = (int *)*loc;
*out = *(d+1);
}

void fcpoke1_(val,loc)
int *val;
int *loc;
{
int *d;
d = (int *)*loc;
*d = *val;
}

void fcpoke2_(val,loc)
int *val;
int *loc;
{
int *d;
d = (int *)*loc;
*(d+1) = *val;
}

//==============================================================


void under0_(log)
int log;
{
}

void invalop_(log)
int log;
{
}

void dbug_(char *mess, long int lmess)
{
char Mess[100];
movstr(Mess,mess,lmess);
dbugger(MANDATORY,"--- %s\n",Mess);
}


void initwebio_(char *pname,long int clen)
{
struct sigaction sigact;
char Message[Pipe_Message_Length];
char *mess = Message;
char Parpid[Pipe_Message_Length-1];
char *ppid = Parpid;

fixlc(program_name,pname,clen);
sprintf( DEBUG_FILE, "%s.dbug", program_name ); // Set program name
setlinebuf(stdout); // unbuffered stdout
sprintf( pipe_name, "%s.fifo", program_name );
dbugger(MANDATORY, "Program: %s is INITIALIZING", program_name);
/*
The following lines pertain to the IPC::Open3 Webio Wrapper.
The must be commented out to use the IPC::Run Webio Wrapper.
*/
// open_pipe(pipe_name, Pipe_Message_Length);

// if(! read_pipe(Message)) {
// dbugger(MANDATORY, "Unable to read PPID from %s, quitting.",pipe_name);
// exit(1);
// }
// close_pipe();
// fixlc(ppid,mess,Pipe_Message_Length-1);
// dbugger(MANDATORY, "ppid value = %s", ppid);
// PPID = atoi(ppid);
// dbugger(MANDATORY, "Program: %s invoked from parent %d process", program_name, PPID);

// sigact.sa_handler = sigusr;
// sigemptyset(&sigact.sa_mask);
// sigact.sa_flags = 0;

// if (sigaction(SIGALRM, &sigact, (void *) NULL))
// dbugger(MANDATORY, "Sigaction - SIGALRM failed");
// if (sigaction(SIGUSR1, &sigact, (void *) NULL))
// dbugger(MANDATORY, "Sigaction - SIGUSR1 failed");
// if (sigaction(SIGUSR2, &sigact, (void *) NULL))
// dbugger(MANDATORY, "Sigaction - SIGUSR2 failed");

// fciohold_();
}

void sigusr(int sig)
{
if (sig == SIGUSR1)
{
dbugger(INFORMATION, "*** USR1 signal received ***");
Wait_Flag = TRUE;
Hold_Flag = FALSE;
}
if (sig == SIGUSR2)
{
dbugger(INFORMATION, "*** USR2 signal received ***");
Wait_Flag = FALSE;
}
if (sig == SIGALRM)
{
dbugger(INFORMATION, "** ALRM has been raised **");
Alarm_Flag = TRUE;
}
dbugger(LOW_LEVEL, "** Signal %d has been raised **", sig);

} /* end sigusr */


void fciohold_(void)
{
float delta = 0;
dbugger(INFORMATION, "--- Starting Wait for USR1 signal ---");
while (Hold_Flag)
{
usleep(Wait_Time);
delta += (float) Wait_Flag/1000000;
}
dbugger(INFORMATION, "=== USR1 signalled after %12.6f sec ===",delta);
}


void fciowait_(void)
{
float delta = 0;
dbugger(INFORMATION, "--- FCIOWAIT: Holding INPUT for USR2 signal ---");
while (Wait_Flag)
{
usleep(Wait_Time);
delta += (float) Wait_Flag/1000000;
}
dbugger(INFORMATION, "=== END FCIOWAIT: USR2 received, releasing INPUT ===",delta);
}

void fcalarmp_(void)
{
// dbugger(INFORMATION, "--- Preparing to send USR2 to %d ---",PPID);
kill(PPID, SIGUSR2);
dbugger(INFORMATION, "+++ USR2 sent to parent %d & Wait_Flag set +++",PPID);
Wait_Flag = TRUE;
}// printf("---in fcpoke_ %16X %16.7G at %8X\n",*d,*d,loc);




void fixlc(char outstr[],char instr[],long int lng)
{
int j = 0;
for(j=0;j<lng;j++)
{
outstr[j] = (char) tolower((int) instr[j]);
}
outstr[lng]=NULL;

return;
}

void movstr(char outstr[],char instr[],long int lng)
{
int j = 0;
for(j=0;j<lng;j++)
{
outstr[j] = instr[j];
}
outstr[lng]=NULL;

return;
}


/* **************************************** */
/* **************************************** */
/* **** Supporting fifo-pipe functions *** */
/* **************************************** */
/* **************************************** */



void open_pipe( char* pipe_name_in, int pipe_message_size )
{
unlink(pipe_name_in);
dbugger(CRITICAL, "Opening Pipe");
if (mkfifo(pipe_name_in, FIFO_MODE) < 0)
{
dbugger(CRITICAL, "Fifo file %s already exists.", pipe_name_in);
if (verify_pipe(pipe_name_in) == BAD)
exit(BAD);
}

if (chmod(pipe_name_in, S_IRWXU | S_IRWXG | S_IRWXO))
dbugger(IMPORTANT, "Fifo file chmod failed");

// if ((Pipe_Handle = open(pipe_name_in, O_RDONLY | O_NONBLOCK)) < 0)
if ((Pipe_Handle = open(pipe_name_in, O_RDONLY )) < 0)
{
dbugger(CRITICAL, "Reading %s open failed.", pipe_name_in);
exit(BAD);
}
dbugger(CRITICAL, "Finished Opening Pipe");
}



int verify_pipe(char * pipe_name)
{
struct stat filestat;

if (stat(pipe_name, &filestat) < 0)
{
dbugger(CRITICAL, "Stat for %s failed.", pipe_name);
return(BAD);
}

if (!S_ISFIFO(filestat.st_mode))
{
dbugger(CRITICAL, "%s filestat is %d, not a pipe.",
pipe_name, filestat.st_mode);
return(BAD);
}

return(GOOD);
}



int read_pipe( char* message_out )
{
int i;
dbugger(MANDATORY, "Preparing to read pipe");
if ((i = read(Pipe_Handle, message_out, Pipe_Message_Length)) > 0)
{
dbugger(MANDATORY, "Read pipe %d bytes: %s", i, message_out);
return 1;
}
dbugger(MANDATORY, "Pipe read FAILED");
return 0;
}


void close_pipe( void )
{
close( Pipe_Handle );
unlink( pipe_name );
}






This is one of only two C-coded files in this library. The other is dbugger. The rest of the library is Gfortran, compiled here with version 5.

I have seen similar errors on other threads, where -fPIC was not used. but as shown, it is being used here. Yet the error still occurs.

I have had no problem with this Make when using 32-bit versions of Ubuntu up through 14.04.

Any ideas anybody?

Thanks in advance.

--beartham

Last edited by beartham; 11-04-2018 at 01:06 PM. Reason: Added blank lines for clarity.
 
Old 11-07-2018, 02:54 PM   #2
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
The command you highlighted has object files as input so it doesn't compile any code, so I believe the switch (-fPIC) has no effect:
Code:
gcc -shared -fPIC *.o -o libdfcs.so
You should find the command that compiles fcsub.c and add -fPIC to it.

I didn't read through the code.

Last edited by KenJackson; 11-07-2018 at 02:57 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
tiff library - linker error nimra Programming 3 10-08-2011 03:54 AM
[SOLVED] Program compiled with g++ -L option uses library from /usr/lib gregorian Linux - General 3 06-29-2011 01:06 AM
Linker error for a singleton class program harishisnow Programming 3 05-06-2011 12:45 AM
Error recompile with fPIC, but fPIC is already as a compilation option eolmsan Linux - Software 4 12-03-2008 11:05 AM
how to find out shared object or ELF binay compiled with -fPIC dayalan_cse Programming 3 11-12-2008 06:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration