NetBSD Problem Report #50277

From www@NetBSD.org  Sat Sep 26 02:36:49 2015
Return-Path: <www@NetBSD.org>
Received: from mail.netbsd.org (mail.netbsd.org [149.20.53.66])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(Client CN "mail.netbsd.org", Issuer "Postmaster NetBSD.org" (verified OK))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 7EC1DA57FE
	for <gnats-bugs@gnats.NetBSD.org>; Sat, 26 Sep 2015 02:36:49 +0000 (UTC)
Message-Id: <20150926023648.00F84A6558@mollari.NetBSD.org>
Date: Sat, 26 Sep 2015 02:36:47 +0000 (UTC)
From: tnn@NetBSD.org
Reply-To: tnn@NetBSD.org
To: gnats-bugs@NetBSD.org
Subject: rtld relocation bug with thread local storage
X-Send-Pr-Version: www-1.0

>Number:         50277
>Category:       toolchain
>Synopsis:       rtld relocation bug with thread local storage
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    toolchain-manager
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat Sep 26 02:40:00 +0000 2015
>Last-Modified:  Sun Sep 27 09:20:00 +0000 2015
>Originator:     Tobias Nygren
>Release:        7.99.21
>Organization:
>Environment:
x86_64
>Description:
Here are the prerequisites to trigger the bug:

- shared library 1, which has a TLS symbol declared as extern
- shared library 2 provides the TLS symbol
- shared library 1 links with shared library 2 to get the TLS symbol
- main application dlopens shared library 1

With the above conditions satisfied the symbol will point to different addresses in each of the libraries' GOT!

>How-To-Repeat:
======= shlib1.c =======

extern __thread int mysym __attribute__((tls_model("initial-exec")));
int* func1() { return &mysym; }

======= shlib2.c =======

__thread int mysym __attribute__((tls_model("initial-exec"))) = 0;
int* func2() { return &mysym; }

======= main.c =======

#include <stdio.h>
#include <dlfcn.h>

int main(void) {
	int* (*func1)();
	int* (*func2)();
	void* handle1 = dlopen("./libshlib1.so", 0);
	void* handle2 = dlopen("./libshlib2.so", RTLD_NOLOAD);
	func1 = dlsym(handle1, "func1");
	func2 = dlsym(handle2, "func2");
	if (func1() != func2())
		printf("Should not happen! %p %p\n", func1(), func2());
}


======= build instructions =======

cc -g -fPIC -o libshlib2.so -shared shlib2.c
cc -g -fPIC -o libshlib1.so -L. -Wl,-R. -lshlib2 -shared shlib1.c
cc -g -fPIC -o main main.c

======= result =======

./main
Should not happen! 0x7f7ff7ffd0c0 0x7f7ff7ffd0bc

>Fix:
Not sure. Some kind of relocation issue. Moving the symbol from the second library to the first library seems to be a workaround.

>Audit-Trail:
From: Matt Thomas <matt@3am-software.com>
To: gnats-bugs@NetBSD.org,
 tnn@netbsd.org
Cc: toolchain-manager@netbsd.org,
 gnats-admin@netbsd.org,
 netbsd-bugs@netbsd.org
Subject: Re: toolchain/50277: rtld relocation bug with thread local storage
Date: Fri, 25 Sep 2015 23:06:08 -0700

 > On Sep 25, 2015, at 7:40 PM, tnn@netbsd.org wrote:
 >=20
 >> Number:         50277
 >> Category:       toolchain
 >> Synopsis:       rtld relocation bug with thread local storage
 >> Confidential:   no
 >> Severity:       non-critical
 >> Priority:       medium
 >> Responsible:    toolchain-manager
 >> State:          open
 >> Class:          sw-bug
 >> Submitter-Id:   net
 >> Arrival-Date:   Sat Sep 26 02:40:00 +0000 2015
 >> Originator:     Tobias Nygren
 >> Release:        7.99.21
 >> Organization:
 >> Environment:
 > x86_64
 >> Description:
 >=20
 > =3D=3D=3D=3D=3D=3D=3D shlib2.c =3D=3D=3D=3D=3D=3D=3D
 >=20
 > __thread int mysym __attribute__((tls_model("initial-exec"))) =3D 0;
 > int* func2() { return &mysym; }
 >=20

 I believe initial-exec is only valid for static programs, not one using =
 shared libraries.=

From: Tobias Nygren <tnn@NetBSD.org>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: toolchain/50277: rtld relocation bug with thread local storage
Date: Sat, 26 Sep 2015 09:45:47 +0200

 On Sat, 26 Sep 2015 06:10:05 +0000 (UTC)
 Matt Thomas <matt@3am-software.com> wrote:

 >  I believe initial-exec is only valid for static programs, not one using =
 >  shared libraries.

 I was told on pkgsrc-users@ last month that it should work, with some
 limitations. It does appear to work in general. Only this corner case
 is broken. (this caused problems for MesaLib's tls dispatch.)

From: Joerg Sonnenberger <joerg@britannica.bec.de>
To: gnats-bugs@NetBSD.org
Cc: toolchain-manager@netbsd.org, gnats-admin@netbsd.org,
	netbsd-bugs@netbsd.org, tnn@NetBSD.org
Subject: Re: toolchain/50277: rtld relocation bug with thread local storage
Date: Sun, 27 Sep 2015 11:18:28 +0200

 On Sat, Sep 26, 2015 at 06:10:05AM +0000, Matt Thomas wrote:
 >  I believe initial-exec is only valid for static programs, not one using
 >  shared libraries.

 There are some limitations with dlopen/dlclose cycles, but it should
 work.

 Joerg

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.39 2013/11/01 18:47:49 spz Exp $
$NetBSD: gnats_config.sh,v 1.8 2006/05/07 09:23:38 tsutsui Exp $
Copyright © 1994-2014 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.