public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* weak and strong aliases for global data fail
@ 2006-05-17 17:02 Peter S. Mazinger
  2006-05-23  0:12 ` Peter S. Mazinger
  2006-05-23 10:29 ` Mike Frysinger
  0 siblings, 2 replies; 8+ messages in thread
From: Peter S. Mazinger @ 2006-05-17 17:02 UTC (permalink / raw)
  To: binutils

[-- Attachment #1: Type: TEXT/PLAIN, Size: 424 bytes --]

Hello!

Attached tests show a case, when a final binary is OK if 
compiled w/ -fPIC (or -fPIE, it does not have to be ET_DYN, but can be), 
else it fails if using aliases (both weak and strong) for global data.

Sorry for the "big" test cases, I couldn't make them smaller.

Peter

-- 
Peter S. Mazinger <ps dot m at gmx dot net>           ID: 0xA5F059F2
Key fingerprint = 92A4 31E1 56BC 3D5A 2D08  BB6E C389 975E A5F0 59F2

[-- Attachment #2: Type: TEXT/PLAIN, Size: 945 bytes --]

CC=gcc

RPATH=$(shell pwd)

all: failing_weak_app failing_strong_app

weak_lib:
	$(CC) -fPIC -shared -o libgetopt_weak.so getopt.c

strong_lib:
	$(CC) -D__STRONG__ -fPIC -shared -o libgetopt_strong.so getopt.c

working_weak_app: weak_lib
	$(CC) -Wl,-rpath,$(RPATH) -fPIC -o hidden_weak hidden.c -L. -lgetopt_weak
	./hidden_weak -o out.o hidden.c

failing_weak_app: weak_lib
	$(CC) -Wl,-rpath,$(RPATH) -fno-PIE -o bad_hidden_weak hidden.c -L. -lgetopt_weak
	./bad_hidden_weak -o out.o hidden.c

working_strong_app: strong_lib
	$(CC) -Wl,-rpath,$(RPATH) -fPIC -o hidden_strong hidden.c -L. -lgetopt_strong
	./hidden_strong -o out.o hidden.c

failing_strong_app: strong_lib
	$(CC) -Wl,-rpath,$(RPATH) -fno-PIE -o bad_hidden_strong hidden.c -L. -lgetopt_strong
	./bad_hidden_strong -o out.o hidden.c

clean:
	rm -f out.o hidden_weak bad_hidden_weak hidden_strong bad_hidden_strong libgetopt_weak.so libgetopt_strong.so

[-- Attachment #3: Type: TEXT/PLAIN, Size: 1973 bytes --]

/* parts taken from qemu/dyngen.c */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

extern char *optarg_test;
extern int optind_test;
extern int getopt_test (int ___argc, char *const *___argv, const char *__shortopts);

enum {
    OUT_GEN_OP,
    OUT_CODE,
    OUT_INDEX_OP,
};

/* load an elf object file */
static int load_object(const char *filename)
{
    return 0;
}

static int gen_file(FILE *outfile, int out_type)
{
    return 0;
}

static void usage(void)
{
    printf("usage: getopt_hidden [-o outfile] [-c] objfile\n"
           "Generate a dynamic code generator from an object file\n"
           "-c     output enum of operations\n"
           "-g     output gen_op_xx() functions\n"
           );
    exit(1);
}

static void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    fprintf(stderr, "getopt_hidden: ");
    vfprintf(stderr, fmt, ap);
    fprintf(stderr, "\n");
    va_end(ap);
    exit(1);
}

int main(int argc, char **argv)
{
    int c, out_type;
    const char *filename, *outfilename;
    FILE *outfile;

    outfilename = "out.c";
    out_type = OUT_CODE;
    for(;;) {
        c = getopt_test(argc, argv, "ho:cg");
        if (c == -1)
            break;
        switch(c) {
        case 'h':
            usage();
            break;
        case 'o':
            outfilename = optarg_test;
            break;
        case 'c':
            out_type = OUT_INDEX_OP;
            break;
        case 'g':
            out_type = OUT_GEN_OP;
            break;
        }
    }
    if (optind_test >= argc)
        usage();
    filename = argv[optind_test];
    outfile = fopen(outfilename, "w");
    if (!outfile)
        error("could not open '%s'", outfilename);

    load_object(filename);
    gen_file(outfile, out_type);
    fclose(outfile);
    return 0;
}

[-- Attachment #4: Type: TEXT/PLAIN, Size: 2444 bytes --]

#include <stdio.h>

/* taken from glibc/include/libc-symbols.h */
#define attribute_hidden __attribute__((visibility ("hidden")))

#define weak_alias(name, aliasname) _weak_alias (name, aliasname)
#define _weak_alias(name, aliasname) \
  extern __typeof (name) aliasname __attribute__ ((weak, alias (#name)));

#define strong_alias(name, aliasname) _strong_alias(name, aliasname)
#define _strong_alias(name, aliasname) \
  extern __typeof (name) aliasname __attribute__ ((alias (#name)));

#ifdef __STRONG__
# define do_alias strong_alias
#else
# define do_alias weak_alias
#endif

static const char missing[] = "%s: option requires an argument -- %c\n";
static const char illegal[] = "%s: illegal option -- %c\n";

int __opterr attribute_hidden = 1;
do_alias(__opterr,opterr_test)
int __optind attribute_hidden = 1;
do_alias(__optind,optind_test)
int __optopt attribute_hidden = 0;
do_alias(__optopt,optopt_test)
char *__optarg attribute_hidden = NULL;
do_alias(__optarg,optarg_test)

int getopt_test(int argc, char * const argv[], const char *optstring)
{
	static const char *o;		/* multi opt position */
	register const char *p;
	register const char *s;
	int retval = -1;

	__optopt = 0;
	__optarg = NULL;

	if (!o) {				/* Not in a multi-option arg. */
		if ((__optind >= argc)	/* No more args? */
			|| ((p = argv[__optind]) == NULL) /* Missing? */
			|| (*p != '-')		/* Not an option? */
			|| (!*++p)			/* "-" case? */
			) {
			goto DONE;
		}
		if ((*p == '-') && (p[1] == 0)) { /* "--" case. */
/* 			++optind; */
/* 			goto DONE; */
			goto NEXTOPT;		/* Less code generated... */
		}
		o = p;
	}

	retval = (unsigned char) *o; /* Avoid problems for char val of -1. */

	if ((*o == ':') || !(s = strchr(optstring, *o))) { /* Illegal option? */
		s = illegal;
		retval = '?';
		goto BAD;
	}
	
	if (s[1] == ':') {			/* Option takes an arg? */
		if (o[1]) {					/* No space between option and arg? */
			__optarg = (char *)(o + 1);
			goto NEXTOPT;
		}

		if (__optind + 1 < argc) {	/* Space between option and arg? */
			__optarg = argv[++__optind];
		} else {				/* Out of args! */
			s = missing;
			retval = ':';
		BAD:
			__optopt = *o;
			if (*optstring != ':') {
				retval = '?';
				if (__opterr) {
					fprintf(stderr, s, argv[0], *o);
				}
			}
		}
	}

	if (!*++o) {
	NEXTOPT:
		o = NULL;
		++__optind;
	}
 DONE:
	return retval;
}

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2006-05-25 21:05 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-05-17 17:02 weak and strong aliases for global data fail Peter S. Mazinger
2006-05-23  0:12 ` Peter S. Mazinger
2006-05-23  9:35   ` Alan Modra
2006-05-24 15:06     ` Peter S. Mazinger
2006-05-24 17:05       ` Alan Modra
2006-05-23 10:29 ` Mike Frysinger
2006-05-24 15:34   ` Peter S. Mazinger
2006-05-26  2:44     ` Mike Frysinger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).