Skip to contentSkip to conversation

weak functions versus conditional compile

U-Boot Graeme Russ 4 participants 4 messages Nov 17, 2008 – Nov 19, 2008

lore
Share & tools
lore
#0 lore raw

I have a quick question I would like the groups opinion on:

What is the rule-of-thumb for defining a function as weak versus using conditional compilation (or #defines)?

For example, I am doing some work on cleaning up the structure and design of the i386 / sc520 code. Specifically, some functionality such as interrupts, timers and CPU reset have more 'powerful' implementations in the sc520.

Should I declare these functions as weak in the core i386 code and use a config #define to override or should I seperate the functions out into seperate source files and use conditional compilation?

Regards,

Graeme

#1 lore raw
Quoted 5 lines · On Monday 17 November 2008 16:17:54 Graeme Russ wrote:

I have a quick question I would like the groups opinion on:

What is the rule-of-thumb for defining a function as weak versus using conditional compilation (or #defines)?

Quoted 8 lines

For example, I am doing some work on cleaning up the structure and design of the i386 / sc520 code. Specifically, some functionality such as interrupts, timers and CPU reset have more 'powerful' implementations in the sc520.

Should I declare these functions as weak in the core i386 code and use a config #define to override or should I seperate the functions out into seperate source files and use conditional compilation?

i wonder if weak functions that are always satisfied and used unconditionally result in larger code, or if this is only a problem for people whose linkers lack lazy relaxation support ?
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20081117/7c8d4b92/attachment.pgp

#2 lore raw
Quoted 8 lines · Mike Frysinger
Quoted 3 lines · On Monday 17 November 2008 16:17:54 Graeme Russ wrote:

Should I declare these functions as weak in the core i386 code and use a config #define to override or should I seperate the functions out into seperate source files and use conditional compilation?

i wonder if weak functions that are always satisfied and used unconditionally result in larger code, or if this is only a problem for people whose linkers lack lazy relaxation support ?

It's probably a problem with linkers that don't support --gc-sections, which is different from relaxation. Also, you definitely need to compile with -ffunction-sections, or the weak functions might end up in the same section as something linked unconditionally, which makes it impossible for the linker to remove them.

Haavard

#3 lore raw
Quoted 10 lines · Haavard Skinnemoen
Quoted 8 lines · Mike Frysinger
Quoted 3 lines · On Monday 17 November 2008 16:17:54 Graeme Russ wrote:

Should I declare these functions as weak in the core i386 code and use a config #define to override or should I seperate the functions out into seperate source files and use conditional compilation?

i wonder if weak functions that are always satisfied and used unconditionally result in larger code, or if this is only a problem for people whose linkers lack lazy relaxation support ?

Quoted 5 lines

It's probably a problem with linkers that don't support --gc-sections, which is different from relaxation. Also, you definitely need to compile with -ffunction-sections, or the weak functions might end up in the same section as something linked unconditionally, which makes it impossible for the linker to remove them.

And you need to fix the relocation not to relocate NULL values, see
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/config/rs6000/eabi.asm?rev=1.13&content-type=text/x-cvsweb-markup
look for __eabi_uconvert.

For fun I once tried to rewrite these functions i C, not tested though:

void __eabi_convert(unsigned long *low, unsigned long *high,
unsigned long addend)

{
	unsigned long len = high - low, val;
	if (!len)
		return;
	low--;
	do {
		val = *++low;
		if (!val)
			continue;
		*low = val + addend;
	} while(--len);
}

void __eabi_uconvert(unsigned long *low, unsigned long *high,
unsigned long addend)

{
	unsigned long len = high - low, val, val2, *v2p;
	if (!len)
		return;
	low--;
	do {
		val = *++low;
		val += addend;
		v2p = (unsigned long *)val;
		*low = val;
		val2 = *v2p;
		val2 += addend;
		*v2p = val2;
	} while(--len);
}

void __eabi_uconvert_org(unsigned long *low, unsigned long *high,
unsigned long addend)

{
	unsigned long len = high - low, val, val2, *v2p;
	if (!len)
		return;
	low--;
	do {
		val = *++low;
		val += addend;
		v2p = (unsigned long *)val;
		val2 = *v2p;
		*low = val;
		val2 += addend;
		*v2p = val2;
	} while(--len);
}