Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-04-2012, 08:01 AM   #1
LQ Newbie
Registered: Mar 2012
Posts: 17

Rep: Reputation: Disabled
ldd functionality in C++

There is given a problem:
I have to write a c++ program what is able to list its own dependencies (like ldd does). Moreover the binary of this c++ program should have suid flag.
I tried the following:
setenv("LD_TRACE_LOADED_OBJECTS", "yes", 1);
execl(binary, binary, (char *)NULL);

It works for binaries without suid flag. With suid flag I got exit status 5.
After tracing the functionality of the original ldd I figured out the following:
LD_TRACE_LOADED_OBJECTS=1 /lib64/ binary_name
But I'm not sure how to implement it in c++ and neither it will work from c++ too?

Any idea?
Old 07-04-2012, 01:20 PM   #2
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 427Reputation: 427Reputation: 427Reputation: 427Reputation: 427

#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
	if(argc < 2) return 1;

	char ld_so[] = "/lib64/";
	setenv("LD_TRACE_LOADED_OBJECTS", "yes", 1);
	execl(ld_so, ld_so, argv[1], (char *)NULL);
	return 0;
} is an executable:
$ /lib64/
You have invoked `', the helper program for shared library executables.
This program usually lives in the file `/lib/', and special directives
in executable files using ELF shared libraries tell the system's program
loader to load the helper program from this file.  This helper program loads
the shared libraries needed by the program executable, prepares the program
to run, and runs it.  You may invoke this helper program directly from the
command line to load and run an ELF executable file; this is like executing
that file itself, but always uses this helper program from the file you
specified, instead of the helper program file specified in the executable
file you run.  This is mostly of use for maintainers to test new versions
of this helper program; chances are you did not intend to run this program.

  --list                list all dependencies and how they are resolved
  --verify              verify that given object really is a dynamically linked
                        object we can handle
  --library-path PATH   use given PATH instead of content of the environment
                        variable LD_LIBRARY_PATH
  --inhibit-rpath LIST  ignore RUNPATH and RPATH information in object names
                        in LIST
  --audit LIST          use objects named in LIST as auditors
1 members found this post helpful.
Old 07-05-2012, 03:14 AM   #3
LQ Newbie
Registered: Mar 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
Old 07-11-2012, 08:30 AM   #4
LQ Newbie
Registered: Mar 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
Hi there again,

I missed a very important part. It should work for itself if it has suid flag :S But this solution gives:

exit status 5

error message. Is there any way to fix this?
Old 07-11-2012, 08:47 AM   #5
LQ Newbie
Registered: Mar 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
To be more accurate: I got the same error message if the calling binary - with suid flag - tried to ldd another binaries. So the emphasis isn't on that we want to ldd ourself but that we are suid.
Old 07-12-2012, 08:18 AM   #6
Senior Member
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,330

Rep: Reputation: 254Reputation: 254Reputation: 254
For SUID applications some of the LD_* variables are ignored for safety reasons. Maybe this is an error because of this.

You can have a look at the source of readelf to get the information:
$ readelf -d demo

Dynamic section at offset 0xf18 contains 22 entries:
  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: []
 0x00000001 (NEEDED)                     Shared library: []
Libraries which are needed by the listed libraries you have to resolve on your own though.


c++, ldd, linux

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
LDD mistery vadiml Linux - General 2 07-17-2009 04:46 AM
Ubuntu 5.10 -> 6.06: diversion of /usr/bin/ldd to /usr/bin/ldd.amd64 by ia32-libs HellSpawn Linux - Software 2 06-04-2006 09:18 PM
about ldd -d jingle_mj Programming 4 09-10-2004 06:25 AM
about ldd -d jingle_mj Linux - General 2 08-25-2004 06:30 AM
ldd simon Linux - General 1 08-02-2001 06:15 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:54 PM.

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