From nobody@FreeBSD.org  Fri Oct 28 06:56:01 2005
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 842E516A41F
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 28 Oct 2005 06:56:01 +0000 (GMT)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [216.136.204.117])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 1693343D45
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 28 Oct 2005 06:56:01 +0000 (GMT)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.13.1/8.13.1) with ESMTP id j9S6u0Y1027133
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 28 Oct 2005 06:56:00 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.13.1/8.13.1/Submit) id j9S6u0nt027130;
	Fri, 28 Oct 2005 06:56:00 GMT
	(envelope-from nobody)
Message-Id: <200510280656.j9S6u0nt027130@www.freebsd.org>
Date: Fri, 28 Oct 2005 06:56:00 GMT
From: Nakata Maho <maho@FreeBSD.org>
To: freebsd-gnats-submit@FreeBSD.org
Subject: support of $ORIGIN in rtld-elf.
X-Send-Pr-Version: www-2.3

>Number:         88117
>Category:       kern
>Synopsis:       [rtld] [patch] support of $ORIGIN in rtld-elf.
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Fri Oct 28 07:00:28 GMT 2005
>Closed-Date:    Thu Mar 19 00:58:34 UTC 2009
>Last-Modified:  Thu Mar 19 00:58:34 UTC 2009
>Originator:     Nakata Maho
>Release:        5.4-RELEASE
>Organization:
FreeBSD.org
>Environment:
FreeBSD satie.private.org 5.4-RELEASE-p6 FreeBSD 5.4-RELEASE-p6 #0: Sat Sep  3 09:47:26 JST 2005     maho@satie.private.org:/usr/src/sys/i386/compile/MAHO  i386
>Description:
if $ORIGIN is passed via -rpath, option of ld, we replace $ORIGIN
by full path of current directory where the execultables or shared
libs exist.

e.g.
% pwd
/u/l/c
gcc -Wl,-rpath,'$ORIGIN' foo.o -ltest1 -ltest2
ldd ./a.out
./a.out
        libc.so.5 => /lib/libc.so.5 (0x123333)
        libtest1.so.6 => /u/l/c/libtest1.so.6 (0x12345678)
        libtest2.so.3 => /u/l/c/libtest2.so.3 (0x90ABCDEF)

etc. we also allow such usage as well, like LD_LIBRARY_PATH.
gcc -Wl,-rpath,'$ORIGIN/../:$ORIGIN' foo.o -ltest1 -ltest2


>How-To-Repeat:
              
>Fix:
              --- /usr/src/libexec/rtld-elf/rtld.c.org        Sun Oct 23 19:11:26 2005
+++ /usr/src/libexec/rtld-elf/rtld.c    Tue Oct 25 14:49:34 2005
@@ -862,6 +862,10 @@
 {
     char *pathname;
     char *name;
+    char *rpath;
+    char *origin_realpath;
+    char *p, *q, *r;
+    int i;

     if (strchr(xname, '/') != NULL) {  /* Hard coded pathname */
        if (xname[0] != '/' && !trust) {
@@ -878,9 +882,31 @@

     dbg(" Searching for \"%s\"", name);

+/* expand $ORIGIN in refobj->rpath if not NULL */
+    rpath=NULL;
+    if(refobj->rpath!=NULL){
+      rpath = xmalloc(PATH_MAX);           /* full rpath with $ORIGIN expanded */
+      origin_realpath = xmalloc(PATH_MAX);
+      realpath(refobj->path, origin_realpath); /* expand $ORIGIN by real path */
+      i=strlen(origin_realpath);
+      while(origin_realpath[i]!='/') {origin_realpath[i--]='\0';} origin_realpath[i]='\0';/* basename */
+      q=(char *)refobj->rpath;
+      p=strstr(q,"$ORIGIN");
+      r=rpath;
+      if(p==NULL) {rpath=(char *)refobj->rpath;}else{
+       while(p!=NULL){
+       for(i=0;q+i<p;i++){ *(r++)=*(q+i);}
+       strcpy(r,origin_realpath);
+       r=r+strlen(origin_realpath);
+       q=p+strlen("$ORIGIN");
+       p=strstr(q,"$ORIGIN");
+       }
+      }
+    }
+
     if ((pathname = search_library_path(name, ld_library_path)) != NULL ||
       (refobj != NULL &&
-      (pathname = search_library_path(name, refobj->rpath)) != NULL) ||
+      (pathname = search_library_path(name, rpath)) != NULL) ||
       (pathname = search_library_path(name, gethints())) != NULL ||
       (pathname = search_library_path(name, STANDARD_LIBRARY_PATH)) != NULL)
        return pathname;


I don't know how to free rpath and origin_realpath...

>Release-Note:
>Audit-Trail:
Date: Mon, 23 Feb 2009 16:38:27 -0700
From: "Sreekanth Vadapalli" <sreekanth_vadapalli@symantec.com>
To: <bug-followup@FreeBSD.org>, <maho@FreeBSD.org>
Subject: Re: kern/88117: [rtld] [patch] support of $ORIGIN in rtld-elf.

Is this problem addressed ? I am seeing the same problem in FreeBSD 6.0
as well. $ORIGIN is not getting expanded at run time (with GNU ld
version 2.15). Is there a patch available ?

Thanks,

Sreekanth.
State-Changed-From-To: open->closed 
State-Changed-By: maho 
State-Changed-When: Thu Mar 19 00:58:00 UTC 2009 
State-Changed-Why:  
Done by kib. 
http://www.freebsd.org/cgi/cvsweb.cgi/src/libexec/rtld-elf/rtld.c?rev=1.130;content-type=text%2Fx-cvsweb-markup 

http://www.freebsd.org/cgi/query-pr.cgi?pr=88117 
>Unformatted:
