From d10027ece7f8b18ff34c0a5427ca78b31c0b38cf Mon Sep 17 00:00:00 2001 From: Olaf Barthel Date: Fri, 20 Aug 2010 15:33:36 +0000 Subject: [PATCH] - Preparations for clib2 1.205 - Added support for ELF shared objects and libraries. This is implemented through constructor/destructor functions, which means that you can use this functionality even in Amiga Exec style shared libraries, with the proper library initialization code to invoke the constructor/destructor functions for you. - Updated uname() to recognize AmigaOS 4.1. - The translation from Unix to Amiga path names did not properly process multiple occurences of "/./" in the path name. Thanks go to Steven Solie for finding the issue. - The detection of "/./" and "/../" patterns in Unix path names to be translated into Amiga path names did not test if it was overrunning the end of the string. - If strcmp(), strncmp() and memcmp() detect a pair of different characters, then the function result must be calculated as if the characters were of type "unsigned char". This is a requirement according to the ISO 'C' (1994) standard. Thanks go to Georg Steger for finding the issue. - The definitions for INT_MIN, INT_MAX and UINT_MAX in no longer use long integer types, as prompted by Steven Solie. git-svn-id: file:///Users/olsen/Code/migration-svn-zu-git/logical-line-staging/clib2/trunk@15204 87f5fb63-7c3d-0410-a384-fd976d0f7a62 --- library/changes | 26 ++++++ library/include/limits.h | 10 +-- library/libc.gmk | 3 +- library/stdlib_shared_libs.c | 150 ++++++++++++++++++++++++++++++++++ library/string_memcmp.c | 18 +++- library/string_strcmp.c | 11 ++- library/string_strncmp.c | 11 ++- library/systeminfo_sysinfo.c | 4 +- library/unistd_translateu2a.c | 18 ++-- library/utsname_uname.c | 6 +- 10 files changed, 232 insertions(+), 25 deletions(-) create mode 100644 library/stdlib_shared_libs.c diff --git a/library/changes b/library/changes index 5014fde..0a9f332 100644 --- a/library/changes +++ b/library/changes @@ -1,3 +1,29 @@ +- Added support for ELF shared objects and libraries. This is implemented through + constructor/destructor functions, which means that you can use this + functionality even in Amiga Exec style shared libraries, with the proper + library initialization code to invoke the constructor/destructor functions + for you. + +- Updated uname() to recognize AmigaOS 4.1. + +- The translation from Unix to Amiga path names did not properly process + multiple occurences of "/./" in the path name. Thanks go to Steven Solie + for finding the issue. + +- The detection of "/./" and "/../" patterns in Unix path names to be + translated into Amiga path names did not test if it was overrunning + the end of the string. + +- If strcmp(), strncmp() and memcmp() detect a pair of different + characters, then the function result must be calculated as if the + characters were of type "unsigned char". This is a requirement + according to the ISO 'C' (1994) standard. Thanks go to Georg Steger + for finding the issue. + +- The definitions for INT_MIN, INT_MAX and UINT_MAX in no + longer use long integer types, as prompted by Steven Solie. + + c.lib 1.204 (11.11.2008) - The memory allocated by malloc() and friends is now of type MEMF_PRIVATE diff --git a/library/include/limits.h b/library/include/limits.h index 75ca853..c394edd 100644 --- a/library/include/limits.h +++ b/library/include/limits.h @@ -1,5 +1,5 @@ /* - * $Id: limits.h,v 1.11 2006-01-08 12:06:14 obarthel Exp $ + * $Id: limits.h,v 1.12 2010-08-20 15:33:36 obarthel Exp $ * * :ts=4 * @@ -78,9 +78,9 @@ /****************************************************************************/ -#define INT_MIN (-2147483647L - 1) -#define INT_MAX 2147483647L -#define UINT_MAX 4294967295UL +#define INT_MIN (-2147483647 - 1) +#define INT_MAX 2147483647 +#define UINT_MAX 4294967295U /****************************************************************************/ @@ -109,7 +109,7 @@ /****************************************************************************/ -#define SSIZE_MAX 2147483647L +#define SSIZE_MAX LONG_MAX /****************************************************************************/ diff --git a/library/libc.gmk b/library/libc.gmk index 05d27dc..5831629 100755 --- a/library/libc.gmk +++ b/library/libc.gmk @@ -1,5 +1,5 @@ # -# $Id: libc.gmk,v 1.4 2006-11-16 14:39:23 obarthel Exp $ +# $Id: libc.gmk,v 1.5 2010-08-20 15:33:36 obarthel Exp $ # # :ts=8 # @@ -248,6 +248,7 @@ C_LIB := \ stdlib_setjmp.o \ stdlib_set_errno.o \ stdlib_set_process_window.o \ + stdlib_shared_libs.o \ stdlib_shell_escape.o \ stdlib_showerror.o \ stdlib_srand.o \ diff --git a/library/stdlib_shared_libs.c b/library/stdlib_shared_libs.c new file mode 100644 index 0000000..090b1cd --- /dev/null +++ b/library/stdlib_shared_libs.c @@ -0,0 +1,150 @@ +/* + * $Id: stdlib_shared_libs.c,v 1.1 2010-08-20 15:33:36 obarthel Exp $ + * + * :ts=4 + * + * Portable ISO 'C' (1994) runtime library for the Amiga computer + * Copyright (c) 2002-2010 by Olaf Barthel + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Neither the name of Olaf Barthel nor the names of contributors + * may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#if defined(__amigaos4__) + +/****************************************************************************/ + +/* The following is not part of the ISO 'C' (1994) standard. */ + +/****************************************************************************/ + +#ifndef _STDLIB_HEADERS_H +#include "stdlib_headers.h" +#endif /* _STDLIB_HEADERS_H */ + +/****************************************************************************/ + +#ifndef _STDLIB_CONSTRUCTOR_H +#include "stdlib_constructor.h" +#endif /* _STDLIB_CONSTRUCTOR_H */ + +/****************************************************************************/ + +#include + +#include +#include + +/****************************************************************************/ + +static VOID shared_libs_init_exit(BOOL init_or_exit) +{ + struct Library * ElfBase; + struct Library * DOSBase = NULL; + struct ElfIFace * IElf = NULL; + struct DOSIFace * IDOS = NULL; + BPTR segment_list; + + ENTER(); + + /* We need elf.library V52.2 or higher. */ + ElfBase = OpenLibrary("elf.library",0); + if(ElfBase == NULL || (ElfBase->lib_Version < 52) || (ElfBase->lib_Version == 52 && ElfBase->lib_Revision < 2)) + goto out; + + IElf = (struct ElfIFace *)GetInterface(ElfBase,"main",1,NULL); + if(IElf == NULL) + goto out; + + DOSBase = OpenLibrary("dos.library",0); + if(DOSBase == NULL) + goto out; + + IDOS = (struct DOSIFace *)GetInterface(DOSBase,"main",1,NULL); + if(IDOS == NULL) + goto out; + + /* Try to find the Elf handle associated with this + program's segment list. */ + segment_list = GetProcSegList(NULL,GPSLF_CLI | GPSLF_SEG); + if(segment_list != ZERO) + { + Elf32_Handle elf_handle = NULL; + + if(GetSegListInfoTags(segment_list, + GSLI_ElfHandle,&elf_handle, + TAG_DONE) == 1) + { + /* Initialize the shared object system. Note that + we have no way of finding out whether this actually + worked... */ + if(elf_handle != NULL) + InitSHLibs(elf_handle,init_or_exit); + } + } + + out: + + if(IDOS != NULL) + DropInterface((struct Interface *)IDOS); + + if(DOSBase != NULL) + CloseLibrary(DOSBase); + + if(IElf != NULL) + DropInterface((struct Interface *)IElf); + + if(ElfBase != NULL) + CloseLibrary(ElfBase); + + LEAVE(); +} + +/****************************************************************************/ + +CLIB_DESTRUCTOR(shared_libs_exit) +{ + ENTER(); + + shared_libs_init_exit(FALSE); + + LEAVE(); +} + +/****************************************************************************/ + +CLIB_CONSTRUCTOR(shared_libs_init) +{ + ENTER(); + + shared_libs_init_exit(TRUE); + + LEAVE(); + + CONSTRUCTOR_SUCCEED(); +} + +/****************************************************************************/ + +#endif /* __amigaos4__ */ diff --git a/library/string_memcmp.c b/library/string_memcmp.c index 57cbb32..77f8a03 100644 --- a/library/string_memcmp.c +++ b/library/string_memcmp.c @@ -1,5 +1,5 @@ /* - * $Id: string_memcmp.c,v 1.7 2006-01-08 12:04:26 obarthel Exp $ + * $Id: string_memcmp.c,v 1.8 2010-08-20 15:33:36 obarthel Exp $ * * :ts=4 * @@ -135,7 +135,14 @@ __memcmp(const char *m1,const char *m2,size_t len) { if((*m1) != (*m2)) { - result = (*m1) - (*m2); + int b1,b2; + + /* The comparison must be performed as if the + bytes were unsigned characters. */ + b1 = *(unsigned char *)m1; + b2 = *(unsigned char *)m2; + + result = b1 - b2; break; } @@ -185,7 +192,12 @@ memcmp(const void *ptr1, const void *ptr2, size_t len) { if((*m1) != (*m2)) { - result = (*m1) - (*m2); + int b1,b2; + + b1 = *(unsigned char *)m1; + b2 = *(unsigned char *)m2; + + result = b1 - b2; break; } diff --git a/library/string_strcmp.c b/library/string_strcmp.c index d9601b3..4413d0f 100644 --- a/library/string_strcmp.c +++ b/library/string_strcmp.c @@ -1,5 +1,5 @@ /* - * $Id: string_strcmp.c,v 1.4 2006-01-08 12:04:27 obarthel Exp $ + * $Id: string_strcmp.c,v 1.5 2010-08-20 15:33:36 obarthel Exp $ * * :ts=4 * @@ -62,6 +62,8 @@ strcmp(const char *s1, const char * s2) if(s1 != s2) { + int c1,c2; + while((*s1) == (*s2)) { if((*s1) == '\0') @@ -71,7 +73,12 @@ strcmp(const char *s1, const char * s2) s2++; } - result = (*s1) - (*s2); + /* The comparison must be performed as if the + characters were unsigned characters. */ + c1 = *(unsigned char *)s1; + c2 = *(unsigned char *)s2; + + result = c1 - c2; } out: diff --git a/library/string_strncmp.c b/library/string_strncmp.c index 62a1c0b..8a4f4e8 100644 --- a/library/string_strncmp.c +++ b/library/string_strncmp.c @@ -1,5 +1,5 @@ /* - * $Id: string_strncmp.c,v 1.4 2006-01-08 12:04:27 obarthel Exp $ + * $Id: string_strncmp.c,v 1.5 2010-08-20 15:33:36 obarthel Exp $ * * :ts=4 * @@ -78,7 +78,14 @@ strncmp(const char *s1, const char *s2, size_t n) } else { - result = (*s1) - (*s2); + int c1,c2; + + /* The comparison must be performed as if the + characters were unsigned characters. */ + c1 = *(unsigned char *)s1; + c2 = *(unsigned char *)s2; + + result = c1 - c2; break; } } diff --git a/library/systeminfo_sysinfo.c b/library/systeminfo_sysinfo.c index 496b6be..3b01fcd 100644 --- a/library/systeminfo_sysinfo.c +++ b/library/systeminfo_sysinfo.c @@ -1,5 +1,5 @@ /* - * $Id: systeminfo_sysinfo.c,v 1.5 2006-09-22 09:02:51 obarthel Exp $ + * $Id: systeminfo_sysinfo.c,v 1.6 2010-08-20 15:33:36 obarthel Exp $ * * :ts=4 * @@ -141,7 +141,7 @@ sysinfo(int cmd,char *buf,long buflen) GetCPUInfoTags(GCIT_VectorUnit,&vecu,TAG_DONE); - if(vecu == VECTORTYPE_ALTIVEC || vecu == VECTORTYPE_VMX) /* AltiVec and VMX are the same. */ + if(vecu == VECTORTYPE_ALTIVEC || vecu == 2 /* VECTORTYPE_VMX == 2 */) /* AltiVec and VMX are the same. */ s = "ppc+altivec ppc common"; else s = "ppc common"; diff --git a/library/unistd_translateu2a.c b/library/unistd_translateu2a.c index d43b337..f025599 100644 --- a/library/unistd_translateu2a.c +++ b/library/unistd_translateu2a.c @@ -1,5 +1,5 @@ /* - * $Id: unistd_translateu2a.c,v 1.11 2006-10-03 16:36:47 obarthel Exp $ + * $Id: unistd_translateu2a.c,v 1.12 2010-08-20 15:33:36 obarthel Exp $ * * :ts=4 * @@ -50,6 +50,7 @@ * ././foo * foo/./baz * foo/./bar/./baz + * foo/./././bar * foo/. * /. * /tmp @@ -260,7 +261,7 @@ __translate_unix_to_amiga_path_name(char const ** name_ptr,struct name_translati D(("name = '%s' (line %ld)",name,__LINE__)); } - /* Ditch all embedded '/./' ('foo/./bar' -> 'foo/bar'). */ + /* Ditch all embedded '/./' ('foo/./bar' -> 'foo/bar', 'foo/././bar' -> 'foo/bar'). */ if(len > 2) { BOOL have_slash_dot_slash = FALSE; @@ -278,10 +279,11 @@ __translate_unix_to_amiga_path_name(char const ** name_ptr,struct name_translati { for(i = j = 0 ; i < len ; i++) { - replace[j++] = name[i]; - - if(name[i] == '/' && name[i + 1] == '.' && name[i + 2] == '/') + while(i < len - 2 && name[i] == '/' && name[i + 1] == '.' && name[i + 2] == '/') i += 2; + + if(i < len) + replace[j++] = name[i]; } len = j; @@ -474,10 +476,10 @@ __translate_unix_to_amiga_path_name(char const ** name_ptr,struct name_translati { for(i = j = 0 ; i < len ; i++) { - replace[j++] = name[i]; - - if(name[i] == '/' && name[i + 1] == '.' && name[i + 2] == '.' && name[i + 3] == '/') + if(i < len - 3 && name[i] == '/' && name[i + 1] == '.' && name[i + 2] == '.' && name[i + 3] == '/') i += 2; + + replace[j++] = name[i]; } len = j; diff --git a/library/utsname_uname.c b/library/utsname_uname.c index 958fa87..4d98db3 100644 --- a/library/utsname_uname.c +++ b/library/utsname_uname.c @@ -1,5 +1,5 @@ /* - * $Id: utsname_uname.c,v 1.7 2006-11-13 09:25:28 obarthel Exp $ + * $Id: utsname_uname.c,v 1.8 2010-08-20 15:33:36 obarthel Exp $ * * :ts=4 * @@ -135,7 +135,9 @@ uname(struct utsname *info) * 45.3 3.9-BB2 */ - if (46 <= Version && Version <= 52) + if (Version == 53) + version_string = "4.1"; + else if (46 <= Version && Version <= 52) version_string = "4.0"; else if (Version == 45) version_string = "3.9";