/[gentoo-alt]/trunk/aix-miscutils/ldd/ldd.cc
Gentoo

Contents of /trunk/aix-miscutils/ldd/ldd.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1634 - (show annotations) (download) (as text)
Wed Jul 9 07:30:57 2008 UTC (6 years, 5 months ago) by haubi
File MIME type: text/x-c++src
File size: 4827 byte(s)
have it compile on linux too, for packaging
1 /* Copyright 2008 Gentoo Foundation
2 * Distributed under the terms of the GNU General Public License v2
3 * Author: Michael Haubenwallner <haubi@gentoo.org>
4 */
5
6 #include <string>
7 #include <vector>
8 #include <map>
9 #include <iostream>
10 #include <errno.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "ObjectReader.h"
15
16 using namespace aixdll;
17
18 typedef ObjectReader::ObjectContainer ObjectContainer;
19 typedef std::map<std::string, ObjectPtr> SeenContainer;
20 typedef std::vector<std::string> MissingContainer;
21
22 static bool collectSharedObjects(ObjectReader& reader, bool shallow
23 , std::string const& path, std::string const& identifier
24 , SeenContainer& read, ObjectContainer& resolved, MissingContainer& unread, SearchPath& runpath
25 )
26 {
27 ObjectContainer objects;
28
29 if (reader.readObjects(path + identifier, identifier, objects) == 0) {
30 return false;
31 }
32
33 ObjectContainer::iterator obj;
34 for(obj = objects.begin(); obj != objects.end(); obj++) {
35 SharedObject* sharedObject = dynamic_cast<SharedObject*>(obj->ptr());
36 if (sharedObject == NULL) {
37 continue;
38 }
39
40 runpath.append(*sharedObject->runpath().ptr());
41 read[sharedObject->key()->asIdentifier()] = *obj;
42 resolved.push_back(*obj);
43
44 if (shallow) continue;
45
46 SharedObject::DependencyContainer const& dependencies =
47 sharedObject->getDependencies();
48 SharedObject::DependencyContainer::const_iterator dep;
49 for(dep = dependencies.begin(); dep != dependencies.end(); dep++) {
50 if ((*dep)->key()) {
51 if (read.find((*dep)->key()->asIdentifier()) != read.end()) {
52 continue;
53 }
54 unread.push_back((*dep)->key()->asIdentifier());
55 } else
56 if ((*dep)->name()) {
57 if (read.find((*dep)->name()->asIdentifier()) != read.end()) {
58 continue;
59 }
60 unread.push_back((*dep)->name()->asIdentifier());
61 }
62 }
63 }
64
65 return true;
66 }
67
68 void ldd_r(ObjectReader& reader, bool shallow, std::string const& filename)
69 {
70 SeenContainer read;
71 MissingContainer unread;
72 ObjectContainer resolved;
73 SearchPath runpath;
74 char const* envLibpath = getenv("LIBPATH");
75 if (envLibpath) {
76 runpath.appendSeparated(envLibpath);
77 }
78
79 if (!collectSharedObjects(reader, false, "", filename, read, resolved, unread, runpath)) {
80 return;
81 }
82
83 resolved.clear();
84
85 while(unread.size() > 0) {
86 MissingContainer::iterator unr = unread.begin();
87 ObjectPathPtr path;
88 ObjectNamePtr name;
89 std::string unrName = *unr;
90 unread.erase(unr);
91 if (read.find(unrName) != read.end()) {
92 continue;
93 }
94 splitIdentifier(unrName, path, name);
95 if (path) {
96 if (!collectSharedObjects(reader, shallow
97 , ""
98 , unrName
99 , read
100 , resolved
101 , unread
102 , runpath
103 )) {
104 resolved.push_back(
105 ObjectPtr(new Object(
106 ObjectKeyPtr(new ObjectKey(path, name))
107 , "", ""
108 ))
109 );
110 }
111 } else {
112 SearchPath::PathContainer::const_iterator p;
113 for(p = runpath.components().begin()
114 ; p != runpath.components().end()
115 ; p++
116 ) {
117 if (collectSharedObjects(reader, shallow
118 , (*p)
119 , name->asIdentifier()
120 , read
121 , resolved
122 , unread
123 , runpath
124 )) {
125 break;
126 }
127 }
128 if (p == runpath.components().end()) {
129 resolved.push_back(
130 ObjectPtr(new Object(
131 ObjectKeyPtr(new ObjectKey(path, name))
132 , "", ""
133 ))
134 );
135 }
136 }
137 }
138
139 for(ObjectContainer::iterator r = resolved.begin()
140 ; r != resolved.end()
141 ; r++
142 ) {
143 std::cout << " "; // tab
144 if ((*r)->getResolved().length() == 0) {
145 std::cout
146 << (*r)->key()->asIdentifier()
147 << " => not found"
148 ;
149 } else
150 if ((*r)->key()->path()) {
151 std::cout << (*r)->getResolved();
152 } else {
153 std::cout
154 << (*r)->key()->asIdentifier()
155 << " => " << (*r)->getResolved();
156 }
157 std::cout << std::endl;
158 }
159 }
160
161 bool ldd(ObjectReader& reader, bool shallow, MissingContainer const& toread)
162 {
163 ObjectContainer objects;
164 bool err = false;
165
166 for(MissingContainer::const_iterator filename = toread.begin()
167 ; filename != toread.end()
168 ; filename++
169 ) {
170 if (reader.readObjects(*filename, *filename, objects) == 0) {
171 std::cerr << "ldd: " << *filename << ": " << strerror(ENOENT) << std::endl;
172 err = true;
173 }
174 }
175
176 for(ObjectContainer::iterator o = objects.begin()
177 ; o != objects.end()
178 ; o++
179 ) {
180 SharedObject *sharedObject = dynamic_cast<SharedObject*>(o->ptr());
181 if (!sharedObject) {
182 continue;
183 }
184 if (objects.size() > 1) {
185 std::cout << (*o)->key()->asIdentifier() << ":" << std::endl;
186 }
187 ldd_r(reader, shallow, (*o)->key()->asIdentifier());
188 }
189
190 return err;
191 }
192
193 int main(int argc, char **argv)
194 {
195 ObjectReader reader;
196 int i;
197 std::string arg;
198 bool shallow = false;
199 MissingContainer toread;
200
201 for(i = 1; i < argc; i++) {
202 arg = argv[i];
203 if (arg == "--shallow") {
204 shallow = true;
205 continue;
206 }
207 toread.push_back(arg);
208 }
209
210 return ldd(reader, shallow, toread);
211 }

  ViewVC Help
Powered by ViewVC 1.1.20