Skip to content

Commit

Permalink
[watcom,libc] Add weak symbol support for OpenWatcom in C Library
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaerr committed Aug 18, 2024
1 parent 947ec1a commit 42a3edd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
5 changes: 3 additions & 2 deletions elkscmd/basic/host.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ float adjust(float f) {
// for float testing compatibility, use same FP formatting routines on host for now
// floats have approx 7 sig figs, 15 for double

#if __ia16__
#if __ia16__ || defined(__WATCOMC__)
#include <sys/linksym.h>
__STDIO_PRINT_FLOATS; // link in libc printf float support

char *host_floatToStr(float f, char *buf) {
__STDIO_PRINT_FLOATS; // link in libc printf float support

sprintf(buf, "%.*g", MATH_PRECISION, (double)f);
return buf;
}
Expand Down
16 changes: 14 additions & 2 deletions libc/include/sys/weaken.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@

#ifdef __GNUC__
/* return address of weak version of passed symbol */
#define _weaken(sym) __extension__ ({ \
#define _weakaddr(sym) __extension__ ({ \
extern __typeof__(sym) sym __attribute__((__weak__)); \
sym; })

#define _weakfn(sym) __extension__ ({ \
extern __typeof__(sym) sym __attribute__((__weak__)); \
sym; })
#endif

#ifdef __WATCOMC__
#define _weaken(sym) (sym) /* FIXME weak symbols not yet implemented */

#if defined(__COMPACT__) || defined(__LARGE__)
/* FIXME: this can fail if weak symbol happens to fall at address zero of segment! */
#define _weakaddr(sym) (((unsigned long)sym) & 0xFFFF)
#else
#define _weakaddr(sym) (sym)
#endif

#define _weakfn(sym) (sym)
#endif

#endif
3 changes: 3 additions & 0 deletions libc/stdio/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ all: $(LIB)

$(LIB): $(LIBOBJS)
$(RM) $@
ifeq "$(COMPILER)" "watcom"
fixomf -w dtostr_,ptostr_ vfprintf.obj
endif
$(AR) $(ARFLAGS_SUB) $@ $(LIBOBJS)

clean:
Expand Down
10 changes: 4 additions & 6 deletions libc/stdio/vfprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,14 @@ vfprintf(FILE *op, const char *fmt, va_list ap)
case 'k': /* Pticks */
usproc:
l = lval? va_arg(ap, unsigned long) : (unsigned long)va_arg(ap, unsigned int);
#ifndef __WATCOMC__
if (*fmt == 'k') {
if (_weaken(ptostr)) {
(_weaken(ptostr))(l, ptmp);
if (_weakaddr(ptostr)) {
(_weakfn(ptostr))(l, ptmp);
preci = -1;
goto printit;
}
/* if precision timing not linked in, display as unsigned */
}
#endif
ptmp = ultostr(l, radix);
if( hash && radix == 8 ) { width = strlen(ptmp)+1; pad='0'; }
goto printit;
Expand Down Expand Up @@ -258,9 +256,9 @@ vfprintf(FILE *op, const char *fmt, va_list ap)
case 'g':
case 'E':
case 'G':
if (_weaken(dtostr))
if (_weakaddr(dtostr))
{
(_weaken(dtostr))(va_arg(ap, double), *fmt, preci, ptmp);
(_weakfn(dtostr))(va_arg(ap, double), *fmt, preci, ptmp);
preci = -1;
goto printit;
}
Expand Down

0 comments on commit 42a3edd

Please sign in to comment.