LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-08-2012, 04:51 AM   #1
fright
LQ Newbie
 
Registered: Nov 2012
Posts: 8

Rep: Reputation: Disabled
Error in compilation : (.text+0x3e2): undefined reference to `elf_setshstrndx'


this is my source code
<


#include<err.h>
#include<fcntl.h>
#include<libelf.h>
#include<stdio.h>
#include<stdlib.h>
#include<sysexits.h>
#include<unistd.h>
uint32_t hash_words[] = {
0x01234567,
0x89abcdef,
0xdeadc0de
};

char string_table[] = {
/* Offset 0 */ '\0',
/* Offset 1 */ '.', 'f', 'o', 'o', '\0',
/* Offset 6 */ '.', 's', 'h', 's', 't',
'r', 't', 'a', 'b', '\0'
};

int
main(int argc , char **argv )
{

int fd;
Elf *e;
Elf_Scn *scn;
Elf_Data *data;
Elf32_Ehdr *ehdr;
Elf32_Phdr *phdr;
Elf32_Shdr *shdr;

if (argc != 2)
errx(EX_USAGE, "usage : %s file-name", argv[0]);

if (elf_version(EV_CURRENT) == EV_NONE)
errx(EX_SOFTWARE, " ELF library initialization "
"failed: %s", elf_errmsg(-1));

if ((fd = open(argv[1], O_WRONLY | O_CREAT, 0777)) < 0)
err(EX_OSERR, "open \%s\" failed", argv[1]);

if ((e = elf_begin(fd, ELF_C_WRITE, NULL)) == NULL)

errx(EX_SOFTWARE, "elf_begin() failed: %s.",
elf_errmsg(-1));

if ((ehdr = elf32_newehdr(e )) == NULL)
errx(EX_SOFTWARE, "elf32_newehdr() failed: %s.",
elf_errmsg(-1));

ehdr-> e_ident [EI_DATA] = ELFDATA2MSB;
ehdr-> e_machine = EM_PPC ; /* 32 - bit PowerPC object */
ehdr-> e_type = ET_EXEC ;

if ((phdr = elf32_newphdr(e, 1)) == NULL)
errx(EX_SOFTWARE, " elf32_newphdr() failed: %s.",
elf_errmsg(-1));

if ((scn = elf_newscn(e)) == NULL )
errx (EX_SOFTWARE, "elf_newscn() failed: %s.",
elf_errmsg(-1));

if ((data = elf_newdata(scn))== NULL)
errx(EX_SOFTWARE, "elf_newdata() failed: %s.",
elf_errmsg(-1));


data->d_align = 4;
data->d_off = 0LL;
data->d_buf = hash_words;
data->d_type = ELF_T_WORD;
data->d_size = sizeof (hash_words);
data->d_version = EV_CURRENT;

if ((shdr = elf32_getshdr(scn )) == NULL)
errx(EX_SOFTWARE, "elf32_getshdr() failed: %s.",
elf_errmsg(-1));

shdr->sh_name = 1;
shdr->sh_type = SHT_HASH;
shdr->sh_flags = SHF_ALLOC;
shdr->sh_entsize = 0;

if ((scn = elf_newscn(e)) == NULL)
errx(EX_SOFTWARE, "elf_newscn() failed : %s.",
elf_errmsg(-1));

if ((data = elf_newdata(scn)) == NULL)
errx(EX_SOFTWARE, "elf_newdata() failed : %s.",
elf_errmsg(-1));

data->d_align = 1;
data->d_buf = string_table ;
data->d_off = 0LL ;
data->d_size = sizeof(string_table);

data->d_type = ELF_T_BYTE;
data->d_version = EV_CURRENT;

if ((shdr = elf32_getshdr(scn)) == NULL)
errx(EX_SOFTWARE, "elf32_getshdr() failed: %s.",
elf_errmsg(-1));

shdr->sh_name = 6;
shdr->sh_type = SHT_STRTAB ;
shdr->sh_flags = SHF_STRINGS | SHF_ALLOC ;
shdr->sh_entsize = 0;
elf_setshstrndx(e, elf_ndxscn(scn));

if (elf_update(e, ELF_C_NULL) < 0)
errx(EX_SOFTWARE, "elf_update(NULL ) failed: %s.",
elf_errmsg(-1));

phdr->p_type = PT_PHDR ;
phdr->p_offset = ehdr-> e_phoff ;
phdr->p_filesz = elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);

(void) elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY);

if (elf_update(e, ELF_C_WRITE) < 0)
errx(EX_SOFTWARE, "elf_update() failed: %s.",
elf_errmsg(-1));

(void) elf_end(e);
(void) close(fd);
exit(EX_OK);
}



after compilation i get following error

/tmp/ccstWCyE.o: In function `main':
prog5.c.text+0x3e2): undefined reference to `elf_setshstrndx'
collect2: ld returned 1 exit status

plz resolve it
 
Old 11-08-2012, 05:43 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Please quote the command you used to link the executable.
Did you add '-lelf' to the command? Why not?
 
Old 11-08-2012, 06:01 AM   #3
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,511

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
It's a header issue, I guess :
No "elf_setshstrndx" in any Linux elf headers ? ( Line 111.)
( And no elf_setshstrndx in libelf: readelf -s libelf* | grep elf_setshstrndx )

http://comments.gmane.org/gmane.comp...ils.devel/1592
Quote 1 :
I believe this is a freebsd libelf extension that isn't in any other
libelf implementation.

Quote 2 :
No libelf I've seen has an elf_setshstrndx function. The only reason you'd
need a helper function is when the section number you need to set is
greater than 0xff00 so that you need to use SHN_XINDEX. We don't have any
helper code in libelf for setting SHN_XINDEX data, only for reading it
(e.g. elf_getshstrndx, which has changed its preferred name to
elf_getshdrstrndx.)
http://www.unix.com/man-page/all/3/elf_setshstrndx/
http://permalink.gmane.org/gmane.com...ng.dwarves/141

.

Last edited by knudfl; 11-08-2012 at 06:10 AM.
 
1 members found this post helpful.
Old 11-08-2012, 08:34 AM   #4
fright
LQ Newbie
 
Registered: Nov 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
i use this command for compilation

cc -o prog2 prog2.c -lelf
 
Old 11-08-2012, 08:38 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Add these options too: -g -W -Wall -Wextra -pedantic -Werror
 
  


Reply



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
[SOLVED] Compilation error - GSL library + MPI Benchmark Library = undefined reference quadpixels Programming 5 07-08-2012 03:33 AM
Undefined Reference error JMJ_coder Programming 6 03-20-2008 01:27 AM
undefined reference to error in C++ knobby67 Programming 3 01-19-2008 10:34 PM
GTK+-2.8.17 Compilation problem:undefined reference to `g_intern_static_string' zakariya Linux - Software 1 06-26-2006 09:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:02 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