aboutsummaryrefslogtreecommitdiff
path: root/elf/dl-object.c
diff options
context:
space:
mode:
Diffstat (limited to 'elf/dl-object.c')
-rw-r--r--elf/dl-object.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/elf/dl-object.c b/elf/dl-object.c
index ed4b059754..f2ef8163ce 100644
--- a/elf/dl-object.c
+++ b/elf/dl-object.c
@@ -20,6 +20,7 @@
#include <errno.h>
#include <string.h>
#include <stdlib.h>
+#include <unistd.h>
#include <elf/ldsodefs.h>
#include <assert.h>
@@ -61,5 +62,68 @@ _dl_new_object (char *realname, const char *libname, int type)
l->l_next = new;
}
+ /* The REALNAME is "" for the main link map. This name must be determined
+ specially. */
+ if (realname[0] == '\0')
+ new->l_origin = NULL;
+ else
+ {
+ char *origin;
+
+ if (realname[0] == '/')
+ {
+ /* It an absolute path. Use it. But we have to make a copy since
+ we strip out the trailing slash. */
+ size_t len = strlen (realname) + 1;
+ origin = malloc (len);
+ if (origin == NULL)
+ origin = (char *) -1;
+ else
+ memcpy (origin, realname, len);
+ }
+ else
+ {
+ size_t realname_len = strlen (realname) + 1;
+ size_t len = 128 + realname_len;
+ char *result = NULL;
+
+ /* Get the current directory name. */
+ origin = malloc (len);
+
+ while (origin != NULL
+ && (result = __getcwd (origin, len - realname_len)) == NULL
+ && errno == ERANGE)
+ {
+ len += 128;
+ origin = (char *) realloc (origin, len);
+ }
+
+ if (result == NULL)
+ {
+ /* We were not able to determine the current directory. */
+ if (origin != NULL)
+ free (origin);
+ origin = (char *) -1;
+ }
+ else
+ {
+ /* Now append the filename. */
+ char *cp = strchr (origin, '\0');
+
+ if (cp [-1] != '/')
+ *cp++ = '/';
+
+ memcpy (cp, realname, realname_len);
+ }
+ }
+
+ if (origin != (char *) -1)
+ /* Now remove the filename and the slash. Do this even if the
+ string is something like "/foo" which leaves an empty string. */
+ *strrchr (origin, '/') = '\0';
+
+ new->l_origin = origin;
+ }
+
return new;
}