| @@ -175,9 +175,11 @@ ST_FUNC char *pstrcat(char *buf, size_t buf_size, const char *s) · 174 unmodified lines |
| 175 | 175 | return buf; |
| 176 | 176 | } |
| 177 | 177 | |
| 178 | | −ST_FUNC char *pstrncpy(char *out, const char *in, size_t num) |
| 178 | +ST_FUNC char *pstrncpy(char *out, size_t buf_size, const char *s, size_t num) |
| 179 | 179 | { |
| 180 | | − memcpy(out, in, num); |
| 180 | + if (num >= buf_size) |
| 181 | + num = buf_size - 1; |
| 182 | + memcpy(out, s, num); |
| 181 | 183 | out[num] = '\0'; |
| 182 | 184 | return out; |
| 183 | 185 | } |
| @@ -206,11 +208,26 @@ ST_FUNC char *tcc_load_text(int fd) · 22 unmodified lines |
| 206 | 208 | { |
| 207 | 209 | int len = lseek(fd, 0, SEEK_END); |
| 208 | 210 | char *buf = load_data(fd, 0, len + 1); |
| 209 | | − if (buf) |
| 210 | | − buf[len] = 0; |
| 211 | + buf[len] = 0; |
| 211 | 212 | return buf; |
| 212 | 213 | } |
| 213 | 214 | |
| 215 | +/* replace *pp by copy of 'str' or NULL */ |
| 216 | +static void tcc_set_str(char **pp, const char *str) |
| 217 | +{ |
| 218 | + tcc_free(*pp); |
| 219 | + *pp = str ? tcc_strdup(str) : NULL; |
| 220 | +} |
| 221 | + |
| 222 | +/* set/append 'str' to *pp (separated by 'sep' unless 0) */ |
| 223 | +static void tcc_concat_str(char **pp, const char *str, int sep) |
| 224 | +{ |
| 225 | + int l = *pp ? strlen(*pp) + !!sep : 0; |
| 226 | + *pp = tcc_realloc(*pp, l + strlen(str) + 1); |
| 227 | + if (l && sep) ((*pp)[l - 1] = sep); |
| 228 | + strcpy(*pp + l, str); |
| 229 | +} |
| 230 | + |
| 214 | 231 | /********************************************************/ |
| 215 | 232 | /* memory management */ |
| 216 | 233 | |
| @@ -519,6 +536,41 @@ ST_FUNC void dynarray_reset(void *pp, int *n) · 302 unmodified lines |
| 519 | 536 | *(void**)pp = NULL; |
| 520 | 537 | } |
| 521 | 538 | |
| 539 | +static void dynarray_split(char ***argv, int *argc, const char *p, int sep) |
| 540 | +{ |
| 541 | + int qot, c; |
| 542 | + CString str; |
| 543 | + for(;;) { |
| 544 | + while (c = (unsigned char)*p, c <= ' ' && c != '\0') |
| 545 | + ++p; |
| 546 | + if (c == '\0') |
| 547 | + break; |
| 548 | + cstr_new(&str); |
| 549 | + qot = 0; |
| 550 | + do { |
| 551 | + ++p; |
| 552 | + if (sep) { /* e.g. to split -Wl,-opt,arg */ |
| 553 | + if (c == sep) |
| 554 | + break; |
| 555 | + } else { |
| 556 | + /* e.g. for tcc_set_options() or "tcc @listfile" */ |
| 557 | + if (c == '\\' && (*p == '"' || *p == '\\')) { |
| 558 | + c = *p++; |
| 559 | + } else if (c == '"') { |
| 560 | + qot ^= 1; |
| 561 | + continue; |
| 562 | + } else if (c <= ' ' && !qot) { |
| 563 | + break; |
| 564 | + } |
| 565 | + } |
| 566 | + cstr_ccat(&str, c); |
| 567 | + } while (c = (unsigned char)*p, c != '\0'); |
| 568 | + cstr_ccat(&str, '\0'); |
| 569 | + //printf("<%s>\n", str.data); |
| 570 | + dynarray_add(argv, argc, str.data); |
| 571 | + } |
| 572 | +} |
| 573 | + |
| 522 | 574 | static void tcc_split_path(TCCState *s, void *p_ary, int *p_nb_ary, const char *in) |
| 523 | 575 | { |
| 524 | 576 | const char *p; |
| @@ -885,9 +937,9 @@ LIBTCCAPI void tcc_delete(TCCState *s1) · 360 unmodified lines |
| 885 | 937 | dynarray_reset(&s1->target_deps, &s1->nb_target_deps); |
| 886 | 938 | dynarray_reset(&s1->pragma_libs, &s1->nb_pragma_libs); |
| 887 | 939 | dynarray_reset(&s1->argv, &s1->argc); |
| 940 | + dynarray_reset(&s1->link_argv, &s1->link_argc); |
| 888 | 941 | cstr_free(&s1->cmdline_defs); |
| 889 | 942 | cstr_free(&s1->cmdline_incl); |
| 890 | | − cstr_free(&s1->linker_arg); |
| 891 | 943 | tcc_free(s1->dState); |
| 892 | 944 | #ifdef TCC_IS_NATIVE |
| 893 | 945 | /* free runtime memory */ |
| @@ -962,6 +1014,17 @@ LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname) · 68 unmodified lines |
| 962 | 1014 | return 0; |
| 963 | 1015 | } |
| 964 | 1016 | |
| 1017 | +LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname) |
| 1018 | +{ |
| 1019 | + tcc_split_path(s, &s->library_paths, &s->nb_library_paths, pathname); |
| 1020 | + return 0; |
| 1021 | +} |
| 1022 | + |
| 1023 | +LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path) |
| 1024 | +{ |
| 1025 | + tcc_set_str(&s->tcc_lib_path, path); |
| 1026 | +} |
| 1027 | + |
| 965 | 1028 | /* add/update a 'DLLReference', Just find if level == -1 */ |
| 966 | 1029 | ST_FUNC DLLReference *tcc_add_dllref(TCCState *s1, const char *dllname, int level) |
| 967 | 1030 | { |
| @@ -988,6 +1051,109 @@ ST_FUNC DLLReference *tcc_add_dllref(TCCState *s1, const char *dllname, int leve · 20 unmodified lines |
| 988 | 1051 | return ref; |
| 989 | 1052 | } |
| 990 | 1053 | |
| 1054 | +static int tcc_add_binary(TCCState *s1, int flags, const char *filename, int fd) |
| 1055 | +{ |
| 1056 | + ElfW(Ehdr) ehdr; |
| 1057 | + int obj_type; |
| 1058 | + const char *saved_filename = s1->current_filename; |
| 1059 | + int ret = 0; |
| 1060 | + |
| 1061 | + s1->current_filename = filename; |
| 1062 | + obj_type = tcc_object_type(fd, &ehdr); |
| 1063 | + lseek(fd, 0, SEEK_SET); |
| 1064 | + |
| 1065 | + switch (obj_type) { |
| 1066 | + |
| 1067 | + case AFF_BINTYPE_REL: |
| 1068 | + ret = tcc_load_object_file(s1, fd, 0); |
| 1069 | + break; |
| 1070 | + |
| 1071 | + case AFF_BINTYPE_AR: |
| 1072 | + ret = tcc_load_archive(s1, fd, !(flags & AFF_WHOLE_ARCHIVE)); |
| 1073 | + break; |
| 1074 | + |
| 1075 | +#if defined TCC_TARGET_UNIX |
| 1076 | + case AFF_BINTYPE_DYN: |
| 1077 | + if (s1->output_type == TCC_OUTPUT_MEMORY) { |
| 1078 | +#ifdef TCC_IS_NATIVE |
| 1079 | + void* dl = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY); |
| 1080 | + if (dl) |
| 1081 | + tcc_add_dllref(s1, filename, 0)->handle = dl; |
| 1082 | + else |
| 1083 | + ret = FILE_NOT_RECOGNIZED; |
| 1084 | +#endif |
| 1085 | + } else |
| 1086 | + ret = tcc_load_dll(s1, fd, filename, (flags & AFF_REFERENCED_DLL) != 0); |
| 1087 | + break; |
| 1088 | + |
| 1089 | + default: |
| 1090 | + /* as GNU ld, consider it is an ld script if not recognized */ |
| 1091 | + ret = tcc_load_ldscript(s1, fd); |
| 1092 | + break; |
| 1093 | + |
| 1094 | +#elif defined TCC_TARGET_MACHO |
| 1095 | + case AFF_BINTYPE_DYN: |
| 1096 | + case_dyn_or_tbd: |
| 1097 | + if (s1->output_type == TCC_OUTPUT_MEMORY) { |
| 1098 | +#ifdef TCC_IS_NATIVE |
| 1099 | + void* dl; |
| 1100 | + const char* soname = filename; |
| 1101 | + char *tmp = 0; |
| 1102 | + if (obj_type != AFF_BINTYPE_DYN) { |
| 1103 | + tmp = macho_tbd_soname(fd); |
| 1104 | + if (tmp) |
| 1105 | + soname = tmp; |
| 1106 | + } |
| 1107 | + dl = dlopen(soname, RTLD_GLOBAL | RTLD_LAZY); |
| 1108 | + if (dl) |
| 1109 | + tcc_add_dllref(s1, soname, 0)->handle = dl; |
| 1110 | + else |
| 1111 | + ret = FILE_NOT_RECOGNIZED; |
| 1112 | + tcc_free(tmp); |
| 1113 | +#endif |
| 1114 | + } else if (obj_type == AFF_BINTYPE_DYN) { |
| 1115 | + ret = macho_load_dll(s1, fd, filename, (flags & AFF_REFERENCED_DLL) != 0); |
| 1116 | + } else { |
| 1117 | + ret = macho_load_tbd(s1, fd, filename, (flags & AFF_REFERENCED_DLL) != 0); |
| 1118 | + } |
| 1119 | + if (ret) |
| 1120 | + ret = FILE_NOT_RECOGNIZED; |
| 1121 | + break; |
| 1122 | + |
| 1123 | + default: |
| 1124 | + { |
| 1125 | + const char *ext = tcc_fileextension(filename); |
| 1126 | + if (!strcmp(ext, ".tbd")) |
| 1127 | + goto case_dyn_or_tbd; |
| 1128 | + if (!strcmp(ext, ".dylib")) { |
| 1129 | + obj_type = AFF_BINTYPE_DYN; |
| 1130 | + goto case_dyn_or_tbd; |
| 1131 | + } |
| 1132 | + ret = FILE_NOT_RECOGNIZED; |
| 1133 | + break; |
| 1134 | + } |
| 1135 | + |
| 1136 | +#elif defined TCC_TARGET_PE |
| 1137 | + default: |
| 1138 | + if (pe_load_file(s1, fd, filename)) |
| 1139 | + ret = FILE_NOT_RECOGNIZED; |
| 1140 | + break; |
| 1141 | +#endif |
| 1142 | + |
| 1143 | +#ifdef TCC_TARGET_COFF |
| 1144 | + case AFF_BINTYPE_C67: |
| 1145 | + ret = tcc_load_coff(s1, fd); |
| 1146 | + break; |
| 1147 | +#endif |
| 1148 | + } |
| 1149 | + |
| 1150 | + close(fd); |
| 1151 | + s1->current_filename = saved_filename; |
| 1152 | + if (ret == FILE_NOT_RECOGNIZED) |
| 1153 | + return tcc_error_noabort("%s: unrecognized file type", filename); |
| 1154 | + return ret; |
| 1155 | +} |
| 1156 | + |
| 991 | 1157 | /* OpenBSD: choose latest from libxxx.so.x.y versions */ |
| 992 | 1158 | #if defined TARGETOS_OpenBSD && !defined _WIN32 |
| 993 | 1159 | #include <glob.h> |
| @@ -1013,11 +1179,34 @@ static int tcc_glob_so(TCCState *s1, const char *pattern, char *buf, int size) · 19 unmodified lines |
| 1013 | 1179 | } |
| 1014 | 1180 | #endif |
| 1015 | 1181 | |
| 1016 | | −static int guess_filetype(const char *filename); |
| 1182 | +static int guess_filetype(const char *filename) |
| 1183 | +{ |
| 1184 | + int filetype = 0; |
| 1185 | + if (1) { |
| 1186 | + /* use a file extension to detect a filetype */ |
| 1187 | + const char *ext = tcc_fileextension(filename); |
| 1188 | + if (ext[0]) { |
| 1189 | + ext++; |
| 1190 | + if (!strcmp(ext, "S")) |
| 1191 | + filetype = AFF_TYPE_ASMPP; |
| 1192 | + else if (!strcmp(ext, "s")) |
| 1193 | + filetype = AFF_TYPE_ASM; |
| 1194 | + else if (!PATHCMP(ext, "c") |
| 1195 | + || !PATHCMP(ext, "h") |
| 1196 | + || !PATHCMP(ext, "i")) |
| 1197 | + filetype = AFF_TYPE_C; |
| 1198 | + else |
| 1199 | + filetype |= AFF_TYPE_BIN; |
| 1200 | + } else { |
| 1201 | + filetype = AFF_TYPE_C; |
| 1202 | + } |
| 1203 | + } |
| 1204 | + return filetype; |
| 1205 | +} |
| 1017 | 1206 | |
| 1018 | 1207 | ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) |
| 1019 | 1208 | { |
| 1020 | | − int fd, ret = -1; |
| 1209 | + int fd; |
| 1021 | 1210 | |
| 1022 | 1211 | #if defined TARGETOS_OpenBSD && !defined _WIN32 |
| 1023 | 1212 | char buf[1024]; |
| @@ -1041,125 +1230,11 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 17 unmodified lines |
| 1041 | 1230 | return FILE_NOT_FOUND; |
| 1042 | 1231 | } |
| 1043 | 1232 | |
| 1044 | | − s1->current_filename = filename; |
| 1045 | | − if (flags & AFF_TYPE_BIN) { |
| 1046 | | − ElfW(Ehdr) ehdr; |
| 1047 | | − int obj_type; |
| 1048 | | − |
| 1049 | | − obj_type = tcc_object_type(fd, &ehdr); |
| 1050 | | − lseek(fd, 0, SEEK_SET); |
| 1051 | | − |
| 1052 | | − switch (obj_type) { |
| 1053 | | − |
| 1054 | | − case AFF_BINTYPE_REL: |
| 1055 | | − ret = tcc_load_object_file(s1, fd, 0); |
| 1056 | | − break; |
| 1057 | | − |
| 1058 | | − case AFF_BINTYPE_AR: |
| 1059 | | − ret = tcc_load_archive(s1, fd, !(flags & AFF_WHOLE_ARCHIVE)); |
| 1060 | | − break; |
| 1061 | | − |
| 1062 | | −#ifdef TCC_TARGET_PE |
| 1063 | | − default: |
| 1064 | | − ret = pe_load_file(s1, fd, filename); |
| 1065 | | − goto check_success; |
| 1066 | | − |
| 1067 | | −#elif defined TCC_TARGET_MACHO |
| 1068 | | − case AFF_BINTYPE_DYN: |
| 1069 | | − case_dyn_or_tbd: |
| 1070 | | − if (s1->output_type == TCC_OUTPUT_MEMORY) { |
| 1071 | | −#ifdef TCC_IS_NATIVE |
| 1072 | | − void* dl; |
| 1073 | | − const char* soname = filename; |
| 1074 | | − if (obj_type != AFF_BINTYPE_DYN) |
| 1075 | | − soname = macho_tbd_soname(filename); |
| 1076 | | − dl = dlopen(soname, RTLD_GLOBAL | RTLD_LAZY); |
| 1077 | | − if (dl) |
| 1078 | | − tcc_add_dllref(s1, soname, 0)->handle = dl, ret = 0; |
| 1079 | | − if (filename != soname) |
| 1080 | | − tcc_free((void *)soname); |
| 1081 | | −#endif |
| 1082 | | − } else if (obj_type == AFF_BINTYPE_DYN) { |
| 1083 | | − ret = macho_load_dll(s1, fd, filename, (flags & AFF_REFERENCED_DLL) != 0); |
| 1084 | | − } else { |
| 1085 | | − ret = macho_load_tbd(s1, fd, filename, (flags & AFF_REFERENCED_DLL) != 0); |
| 1086 | | − } |
| 1087 | | − goto check_success; |
| 1088 | | − default: |
| 1089 | | − { |
| 1090 | | − const char *ext = tcc_fileextension(filename); |
| 1091 | | − if (!strcmp(ext, ".tbd")) |
| 1092 | | − goto case_dyn_or_tbd; |
| 1093 | | − if (!strcmp(ext, ".dylib")) { |
| 1094 | | − obj_type = AFF_BINTYPE_DYN; |
| 1095 | | − goto case_dyn_or_tbd; |
| 1096 | | − } |
| 1097 | | − goto check_success; |
| 1098 | | − } |
| 1099 | | − |
| 1100 | | −#else /* unix */ |
| 1101 | | − case AFF_BINTYPE_DYN: |
| 1102 | | − if (s1->output_type == TCC_OUTPUT_MEMORY) { |
| 1103 | | −#ifdef TCC_IS_NATIVE |
| 1104 | | − void* dl = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY); |
| 1105 | | − if (dl) |
| 1106 | | − tcc_add_dllref(s1, filename, 0)->handle = dl, ret = 0; |
| 1107 | | −#endif |
| 1108 | | − } else |
| 1109 | | − ret = tcc_load_dll(s1, fd, filename, (flags & AFF_REFERENCED_DLL) != 0); |
| 1110 | | − break; |
| 1111 | | − |
| 1112 | | − default: |
| 1113 | | − /* as GNU ld, consider it is an ld script if not recognized */ |
| 1114 | | − ret = tcc_load_ldscript(s1, fd); |
| 1115 | | − goto check_success; |
| 1233 | + if (flags & AFF_TYPE_BIN) |
| 1234 | + return tcc_add_binary(s1, flags, filename, fd); |
| 1116 | 1235 | |
| 1117 | | −#endif /* pe / macos / unix */ |
| 1118 | | − |
| 1119 | | −check_success: |
| 1120 | | − if (ret < 0) |
| 1121 | | − tcc_error_noabort("%s: unrecognized file type", filename); |
| 1122 | | − break; |
| 1123 | | − |
| 1124 | | −#ifdef TCC_TARGET_COFF |
| 1125 | | − case AFF_BINTYPE_C67: |
| 1126 | | − ret = tcc_load_coff(s1, fd); |
| 1127 | | − break; |
| 1128 | | −#endif |
| 1129 | | − } |
| 1130 | | − close(fd); |
| 1131 | | − } else { |
| 1132 | | − /* update target deps */ |
| 1133 | | − dynarray_add(&s1->target_deps, &s1->nb_target_deps, tcc_strdup(filename)); |
| 1134 | | − ret = tcc_compile(s1, flags, filename, fd); |
| 1135 | | − } |
| 1136 | | − s1->current_filename = NULL; |
| 1137 | | − return ret; |
| 1138 | | −} |
| 1139 | | − |
| 1140 | | −static int guess_filetype(const char *filename) |
| 1141 | | −{ |
| 1142 | | − int filetype = 0; |
| 1143 | | − if (1) { |
| 1144 | | − /* use a file extension to detect a filetype */ |
| 1145 | | − const char *ext = tcc_fileextension(filename); |
| 1146 | | − if (ext[0]) { |
| 1147 | | − ext++; |
| 1148 | | − if (!strcmp(ext, "S")) |
| 1149 | | − filetype = AFF_TYPE_ASMPP; |
| 1150 | | − else if (!strcmp(ext, "s")) |
| 1151 | | − filetype = AFF_TYPE_ASM; |
| 1152 | | − else if (!PATHCMP(ext, "c") |
| 1153 | | − || !PATHCMP(ext, "h") |
| 1154 | | − || !PATHCMP(ext, "i")) |
| 1155 | | − filetype = AFF_TYPE_C; |
| 1156 | | − else |
| 1157 | | − filetype |= AFF_TYPE_BIN; |
| 1158 | | − } else { |
| 1159 | | − filetype = AFF_TYPE_C; |
| 1160 | | − } |
| 1161 | | − } |
| 1162 | | − return filetype; |
| 1236 | + dynarray_add(&s1->target_deps, &s1->nb_target_deps, tcc_strdup(filename)); |
| 1237 | + return tcc_compile(s1, flags, filename, fd); |
| 1163 | 1238 | } |
| 1164 | 1239 | |
| 1165 | 1240 | LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename) |
| @@ -1167,12 +1242,6 @@ LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename) · 1 unmodified line |
| 1167 | 1242 | return tcc_add_file_internal(s, filename, s->filetype | AFF_PRINT_ERROR); |
| 1168 | 1243 | } |
| 1169 | 1244 | |
| 1170 | | −LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname) |
| 1171 | | −{ |
| 1172 | | − tcc_split_path(s, &s->library_paths, &s->nb_library_paths, pathname); |
| 1173 | | − return 0; |
| 1174 | | −} |
| 1175 | | − |
| 1176 | 1245 | static int tcc_add_library_internal(TCCState *s1, const char *fmt, |
| 1177 | 1246 | const char *filename, int flags, char **paths, int nb_paths) |
| 1178 | 1247 | { |
| @@ -1186,7 +1255,8 @@ static int tcc_add_library_internal(TCCState *s1, const char *fmt, · 7 unmodified lines |
| 1186 | 1255 | return ret; |
| 1187 | 1256 | } |
| 1188 | 1257 | if (flags & AFF_PRINT_ERROR) |
| 1189 | | − tcc_error_noabort("library '%s' not found", filename); |
| 1258 | + tcc_error_noabort("%s '%s' not found", |
| 1259 | + flags & AFF_TYPE_LIB ? "library" : "file", filename); |
| 1190 | 1260 | return FILE_NOT_FOUND; |
| 1191 | 1261 | } |
| 1192 | 1262 | |
| @@ -1206,7 +1276,7 @@ ST_FUNC int tcc_add_support(TCCState *s1, const char *filename) · 13 unmodified lines |
| 1206 | 1276 | return tcc_add_dll(s1, filename, AFF_PRINT_ERROR); |
| 1207 | 1277 | } |
| 1208 | 1278 | |
| 1209 | | −#if !defined TCC_TARGET_PE && !defined TCC_TARGET_MACHO |
| 1279 | +#ifdef TCC_TARGET_UNIX |
| 1210 | 1280 | ST_FUNC int tcc_add_crt(TCCState *s1, const char *filename) |
| 1211 | 1281 | { |
| 1212 | 1282 | return tcc_add_library_internal(s1, "%s/%s", |
| @@ -1230,12 +1300,12 @@ LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname) · 17 unmodified lines |
| 1230 | 1300 | "%s/lib%s.a", |
| 1231 | 1301 | NULL |
| 1232 | 1302 | }; |
| 1303 | + int flags = AFF_TYPE_LIB | (s->filetype & AFF_WHOLE_ARCHIVE); |
| 1233 | 1304 | /* if libraryname begins with a colon, it means search lib paths for |
| 1234 | 1305 | exactly the following file, without lib prefix or anything */ |
| 1235 | 1306 | if (*libraryname == ':') { |
| 1236 | 1307 | libraryname++; |
| 1237 | 1308 | } else { |
| 1238 | | − int flags = s->filetype & AFF_WHOLE_ARCHIVE; |
| 1239 | 1309 | const char * const *pp = libs; |
| 1240 | 1310 | if (s->static_link) |
| 1241 | 1311 | pp += sizeof(libs) / sizeof(*libs) - 2; /* only "%s/lib%s.a" */ |
| @@ -1247,7 +1317,8 @@ LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname) · 5 unmodified lines |
| 1247 | 1317 | ++pp; |
| 1248 | 1318 | } |
| 1249 | 1319 | } |
| 1250 | | − return tcc_add_dll(s, libraryname, AFF_PRINT_ERROR); |
| 1320 | + /* fallback to try file without pre- or sufffix */ |
| 1321 | + return tcc_add_dll(s, libraryname, flags | AFF_PRINT_ERROR); |
| 1251 | 1322 | } |
| 1252 | 1323 | |
| 1253 | 1324 | /* handle #pragma comment(lib,) */ |
| @@ -1258,30 +1329,6 @@ ST_FUNC void tcc_add_pragma_libs(TCCState *s1) · 4 unmodified lines |
| 1258 | 1329 | tcc_add_library(s1, s1->pragma_libs[i]); |
| 1259 | 1330 | } |
| 1260 | 1331 | |
| 1261 | | −LIBTCCAPI int tcc_add_symbol(TCCState *s1, const char *name, const void *val) |
| 1262 | | −{ |
| 1263 | | −#ifdef TCC_TARGET_PE |
| 1264 | | − /* On x86_64 'val' might not be reachable with a 32bit offset. |
| 1265 | | − So it is handled here as if it were in a DLL. */ |
| 1266 | | − pe_putimport(s1, 0, name, (uintptr_t)val); |
| 1267 | | −#else |
| 1268 | | − char buf[256]; |
| 1269 | | − if (s1->leading_underscore) { |
| 1270 | | − buf[0] = '_'; |
| 1271 | | − pstrcpy(buf + 1, sizeof(buf) - 1, name); |
| 1272 | | − name = buf; |
| 1273 | | − } |
| 1274 | | − set_global_sym(s1, name, NULL, (addr_t)(uintptr_t)val); /* NULL: SHN_ABS */ |
| 1275 | | −#endif |
| 1276 | | − return 0; |
| 1277 | | −} |
| 1278 | | − |
| 1279 | | −LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path) |
| 1280 | | −{ |
| 1281 | | − tcc_free(s->tcc_lib_path); |
| 1282 | | − s->tcc_lib_path = tcc_strdup(path); |
| 1283 | | −} |
| 1284 | | − |
| 1285 | 1332 | /********************************************************/ |
| 1286 | 1333 | /* options parser */ |
| 1287 | 1334 | |
| @@ -1300,224 +1347,177 @@ static int strstart(const char *val, const char **str) · 12 unmodified lines |
| 1300 | 1347 | return 1; |
| 1301 | 1348 | } |
| 1302 | 1349 | |
| 1303 | | −/* Like strstart, but automatically takes into account that ld options can |
| 1304 | | − * |
| 1305 | | − * - start with double or single dash (e.g. '--soname' or '-soname') |
| 1306 | | − * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so' |
| 1307 | | − * or '-Wl,-soname=x.so') |
| 1308 | | − * |
| 1309 | | − * you provide `val` always in 'option[=]' form (no leading -) |
| 1310 | | − */ |
| 1311 | | −static int link_option(const char *str, const char *val, const char **ptr) |
| 1350 | +struct lopt { |
| 1351 | + TCCState *s; |
| 1352 | + const char *opt, *arg; |
| 1353 | + int match; |
| 1354 | +}; |
| 1355 | + |
| 1356 | +/* match linker option */ |
| 1357 | +static int link_option(struct lopt *o, const char *q) |
| 1312 | 1358 | { |
| 1313 | | − const char *p, *q; |
| 1314 | | − int ret; |
| 1359 | + const char *p = o->opt; |
| 1360 | + int c; |
| 1315 | 1361 | |
| 1316 | 1362 | /* there should be 1 or 2 dashes */ |
| 1317 | | − if (*str++ != '-') |
| 1363 | + if (*p++ != '-') |
| 1318 | 1364 | return 0; |
| 1319 | | − if (*str == '-') |
| 1320 | | − str++; |
| 1321 | | − |
| 1322 | | − /* then str & val should match (potentially up to '=') */ |
| 1323 | | − p = str; |
| 1324 | | − q = val; |
| 1325 | | − |
| 1326 | | − ret = 1; |
| 1327 | | − if (q[0] == '?') { |
| 1328 | | − ++q; |
| 1329 | | − if (strstart("no-", &p)) |
| 1330 | | − ret = -1; |
| 1331 | | − } |
| 1332 | | − |
| 1333 | | − while (*q != '\0' && *q != '=') { |
| 1334 | | − if (*p != *q) |
| 1335 | | − return 0; |
| 1365 | + if (*p == '-') |
| 1336 | 1366 | p++; |
| 1337 | | − q++; |
| 1367 | + while ((c = *q) == *p) { |
| 1368 | + if (c == '\0') |
| 1369 | + goto succ; /* -Wl,-opt */ |
| 1370 | + ++p; |
| 1371 | + if (c == '=') |
| 1372 | + goto succ; /* -Wl,-opt=arg */ |
| 1373 | + ++q; |
| 1338 | 1374 | } |
| 1339 | | − |
| 1340 | | − /* '=' near eos means ',' or '=' is ok */ |
| 1341 | | − if (*q == '=') { |
| 1342 | | − if (*p == 0) |
| 1343 | | − *ptr = p; |
| 1344 | | − if (*p != ',' && *p != '=') |
| 1345 | | − return 0; |
| 1346 | | − p++; |
| 1347 | | − } else if (*p && *p != ',') { |
| 1348 | | − return 0; |
| 1375 | + if (c == '=' || c == ':') { |
| 1376 | + if (*p == '\0') { |
| 1377 | + if (o->s->link_optind + 1 < o->s->link_argc) { |
| 1378 | + p = o->s->link_argv[++o->s->link_optind]; |
| 1379 | + goto succ; /* -Wl,-opt,arg */ |
| 1380 | + } |
| 1381 | + o->match = 1; /* -Wl,-opt -Wl,arg */ |
| 1382 | + } else if (c == ':') |
| 1383 | + goto succ; /* -Wl,-Iarg */ |
| 1349 | 1384 | } |
| 1350 | | − *ptr = p; |
| 1351 | | − return ret; |
| 1352 | | −} |
| 1353 | | − |
| 1354 | | −static int link_arg(const char *opt, const char *str) |
| 1355 | | −{ |
| 1356 | | − int l = strlen(opt); |
| 1357 | | − return 0 == strncmp(opt, str, l) && (str[l] == '\0' || str[l] == ','); |
| 1385 | + return 0; |
| 1386 | +succ: |
| 1387 | + o->arg = p; |
| 1388 | + //printf("set %s '%s'\n", o->opt, o->arg); |
| 1389 | + return 1; |
| 1358 | 1390 | } |
| 1359 | 1391 | |
| 1360 | | −static const char *skip_linker_arg(const char **str) |
| 1361 | | −{ |
| 1362 | | − const char *s1 = *str; |
| 1363 | | − const char *s2 = strchr(s1, ','); |
| 1364 | | − *str = s2 ? s2++ : (s2 = s1 + strlen(s1)); |
| 1365 | | − return s2; |
| 1366 | | −} |
| 1367 | | − |
| 1368 | | −static void copy_linker_arg(char **pp, const char *s, int sep) |
| 1369 | | −{ |
| 1370 | | − const char *q = s; |
| 1371 | | − char *p = *pp; |
| 1372 | | − int l = 0; |
| 1373 | | − if (p && sep) |
| 1374 | | − p[l = strlen(p)] = sep, ++l; |
| 1375 | | − skip_linker_arg(&q); |
| 1376 | | − pstrncpy(l + (*pp = tcc_realloc(p, q - s + l + 1)), s, q - s); |
| 1377 | | −} |
| 1378 | | − |
| 1379 | | −static void args_parser_add_file(TCCState *s, const char* filename, int filetype) |
| 1380 | | −{ |
| 1381 | | − struct filespec *f = tcc_malloc(sizeof *f + strlen(filename)); |
| 1382 | | − f->type = filetype; |
| 1383 | | − strcpy(f->name, filename); |
| 1384 | | − dynarray_add(&s->files, &s->nb_files, f); |
| 1385 | | −} |
| 1392 | +static void args_parser_add_file(TCCState *s, const char* filename, int filetype); |
| 1386 | 1393 | |
| 1387 | 1394 | /* set linker options */ |
| 1388 | | −static int tcc_set_linker(TCCState *s, const char *option) |
| 1395 | +static int tcc_set_linker(TCCState *s, const char *optarg) |
| 1389 | 1396 | { |
| 1390 | 1397 | TCCState *s1 = s; |
| 1391 | | − while (*option) { |
| 1392 | 1398 | |
| 1393 | | − const char *p = NULL; |
| 1399 | + dynarray_split(&s1->link_argv, &s1->link_argc, optarg, ','); |
| 1400 | + |
| 1401 | + while (s->link_optind < s->link_argc) { |
| 1394 | 1402 | char *end = NULL; |
| 1395 | 1403 | int ignoring = 0; |
| 1396 | | − int ret; |
| 1404 | + struct lopt o = {0}; |
| 1405 | + o.s = s; |
| 1406 | + o.opt = s->link_argv[s->link_optind]; |
| 1397 | 1407 | |
| 1398 | | − if (link_option(option, "Bsymbolic", &p)) { |
| 1408 | + if (link_option(&o, "Bsymbolic")) { |
| 1399 | 1409 | s->symbolic = 1; |
| 1400 | | − } else if (link_option(option, "nostdlib", &p)) { |
| 1410 | + } else if (link_option(&o, "nostdlib")) { |
| 1401 | 1411 | s->nostdlib_paths = 1; |
| 1402 | | − } else if (link_option(option, "e=", &p) |
| 1403 | | − || link_option(option, "entry=", &p)) { |
| 1404 | | − copy_linker_arg(&s->elf_entryname, p, 0); |
| 1405 | | − } else if (link_option(option, "fini=", &p)) { |
| 1406 | | − copy_linker_arg(&s->fini_symbol, p, 0); |
| 1407 | | − ignoring = 1; |
| 1408 | | − } else if (link_option(option, "image-base=", &p) |
| 1409 | | − || link_option(option, "Ttext=", &p)) { |
| 1410 | | − s->text_addr = strtoull(p, &end, 16); |
| 1412 | + } else if (link_option(&o, "e=") || link_option(&o, "entry=")) { |
| 1413 | + tcc_set_str(&s->elf_entryname, o.arg); |
| 1414 | + } else if (link_option(&o, "image-base=") || link_option(&o, "Ttext=")) { |
| 1415 | + s->text_addr = strtoull(o.arg, &end, 16); |
| 1411 | 1416 | s->has_text_addr = 1; |
| 1412 | | − } else if (link_option(option, "init=", &p)) { |
| 1413 | | − copy_linker_arg(&s->init_symbol, p, 0); |
| 1417 | + } else if (link_option(&o, "init=")) { |
| 1418 | + tcc_set_str(&s->init_symbol, o.arg); |
| 1419 | + ignoring = 1; |
| 1420 | + } else if (link_option(&o, "fini=")) { |
| 1421 | + tcc_set_str(&s->fini_symbol, o.arg); |
| 1414 | 1422 | ignoring = 1; |
| 1415 | | − } else if (link_option(option, "Map=", &p)) { |
| 1416 | | − copy_linker_arg(&s->mapfile, p, 0); |
| 1423 | + } else if (link_option(&o, "Map=")) { |
| 1424 | + tcc_set_str(&s->mapfile, o.arg); |
| 1417 | 1425 | ignoring = 1; |
| 1418 | | − } else if (link_option(option, "oformat=", &p)) { |
| 1419 | | −#if defined(TCC_TARGET_PE) |
| 1420 | | − if (strstart("pe-", &p)) { |
| 1426 | + } else if (link_option(&o, "oformat=")) { |
| 1427 | +#if defined TCC_TARGET_PE |
| 1428 | + if (0 == strncmp("pe-", o.arg, 3)) |
| 1421 | 1429 | #elif PTR_SIZE == 8 |
| 1422 | | − if (strstart("elf64-", &p)) { |
| 1430 | + if (0 == strncmp("elf64-", o.arg, 6)) |
| 1423 | 1431 | #else |
| 1424 | | − if (strstart("elf32-", &p)) { |
| 1432 | + if (0 == strncmp("elf32-", o.arg, 6)) |
| 1425 | 1433 | #endif |
| 1426 | 1434 | s->output_format = TCC_OUTPUT_FORMAT_ELF; |
| 1427 | | − } else if (link_arg("binary", p)) { |
| 1435 | + else if (0==strcmp("binary", o.arg)) |
| 1428 | 1436 | s->output_format = TCC_OUTPUT_FORMAT_BINARY; |
| 1429 | | −#ifdef TCC_TARGET_COFF |
| 1430 | | − } else if (link_arg("coff", p)) { |
| 1437 | +#if defined TCC_TARGET_COFF |
| 1438 | + else if (0==strcmp("coff", o.arg)) |
| 1431 | 1439 | s->output_format = TCC_OUTPUT_FORMAT_COFF; |
| 1432 | 1440 | #endif |
| 1433 | | − } else |
| 1441 | + else |
| 1434 | 1442 | goto err; |
| 1435 | | − |
| 1436 | | − } else if (link_option(option, "as-needed", &p)) { |
| 1437 | | − ignoring = 1; |
| 1438 | | − } else if (link_option(option, "O", &p)) { |
| 1439 | | − ignoring = 1; |
| 1440 | | − } else if (link_option(option, "export-all-symbols", &p)) { |
| 1441 | | − s->rdynamic = 1; |
| 1442 | | − } else if (link_option(option, "export-dynamic", &p)) { |
| 1443 | + } else if (link_option(&o, "export-all-symbols") |
| 1444 | + || link_option(&o, "export-dynamic")) { |
| 1443 | 1445 | s->rdynamic = 1; |
| 1444 | | − } else if (link_option(option, "rpath=", &p)) { |
| 1445 | | − copy_linker_arg(&s->rpath, p, ':'); |
| 1446 | | − } else if (link_option(option, "dynamic-linker=", &p) |
| 1447 | | − || link_option(option, "I=", &p)) { |
| 1448 | | − copy_linker_arg(&s->elfint, p, 0); |
| 1449 | | − } else if (strncmp("-I/", option, 3) == 0 |
| 1450 | | − || strncmp("-I.", option, 3) == 0) { |
| 1451 | | − p = option; |
| 1452 | | − copy_linker_arg(&s->elfint, option + 2, 0); |
| 1453 | | − } else if (link_option(option, "enable-new-dtags", &p)) { |
| 1446 | + } else if (link_option(&o, "rpath=")) { |
| 1447 | + tcc_concat_str(&s->rpath, o.arg, ':'); |
| 1448 | + } else if (link_option(&o, "dynamic-linker=") || link_option(&o, "I:")) { |
| 1449 | + tcc_set_str(&s->elfint, o.arg); |
| 1450 | + } else if (link_option(&o, "enable-new-dtags")) { |
| 1454 | 1451 | s->enable_new_dtags = 1; |
| 1455 | | − } else if (link_option(option, "section-alignment=", &p)) { |
| 1456 | | − s->section_align = strtoul(p, &end, 16); |
| 1457 | | − } else if (link_option(option, "soname=", &p)) { |
| 1458 | | − copy_linker_arg(&s->soname, p, 0); |
| 1459 | | − } else if (link_option(option, "install_name=", &p)) { |
| 1460 | | − copy_linker_arg(&s->soname, p, 0); |
| 1452 | + } else if (link_option(&o, "section-alignment=")) { |
| 1453 | + s->section_align = strtoul(o.arg, &end, 16); |
| 1454 | + } else if (link_option(&o, "soname=") || link_option(&o, "install_name=")) { |
| 1455 | + tcc_set_str(&s->soname, o.arg); |
| 1456 | + } else if (link_option(&o, "whole-archive")) { |
| 1457 | + s->filetype |= AFF_WHOLE_ARCHIVE; |
| 1458 | + } else if (link_option(&o, "no-whole-archive")) { |
| 1459 | + s->filetype &= ~AFF_WHOLE_ARCHIVE; |
| 1461 | 1460 | #ifdef TCC_TARGET_PE |
| 1462 | | − } else if (link_option(option, "large-address-aware", &p)) { |
| 1461 | + } else if (link_option(&o, "large-address-aware")) { |
| 1463 | 1462 | s->pe_characteristics |= 0x20; |
| 1464 | | − } else if (link_option(option, "file-alignment=", &p)) { |
| 1465 | | − s->pe_file_align = strtoul(p, &end, 16); |
| 1466 | | − } else if (link_option(option, "stack=", &p)) { |
| 1467 | | − s->pe_stack_size = strtoul(p, &end, 10); |
| 1468 | | − } else if (link_option(option, "subsystem=", &p)) { |
| 1463 | + } else if (link_option(&o, "file-alignment=")) { |
| 1464 | + s->pe_file_align = strtoul(o.arg, &end, 16); |
| 1465 | + } else if (link_option(&o, "stack=")) { |
| 1466 | + s->pe_stack_size = strtoul(o.arg, &end, 10); |
| 1467 | + } else if (link_option(&o, "subsystem=")) { |
| 1469 | 1468 | #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64) |
| 1470 | | − if (link_arg("native", p)) { |
| 1469 | + if (0==strcmp("native", o.arg)) { |
| 1471 | 1470 | s->pe_subsystem = 1; |
| 1472 | | − } else if (link_arg("console", p)) { |
| 1471 | + } else if (0==strcmp("console", o.arg)) { |
| 1473 | 1472 | s->pe_subsystem = 3; |
| 1474 | | − } else if (link_arg("gui", p) || link_arg("windows", p)) { |
| 1473 | + } else if (0==strcmp("gui", o.arg) || 0==strcmp("windows", o.arg)) { |
| 1475 | 1474 | s->pe_subsystem = 2; |
| 1476 | | − } else if (link_arg("posix", p)) { |
| 1475 | + } else if (0==strcmp("posix", o.arg)) { |
| 1477 | 1476 | s->pe_subsystem = 7; |
| 1478 | | − } else if (link_arg("efiapp", p)) { |
| 1477 | + } else if (0==strcmp("efiapp", o.arg)) { |
| 1479 | 1478 | s->pe_subsystem = 10; |
| 1480 | | − } else if (link_arg("efiboot", p)) { |
| 1479 | + } else if (0==strcmp("efiboot", o.arg)) { |
| 1481 | 1480 | s->pe_subsystem = 11; |
| 1482 | | − } else if (link_arg("efiruntime", p)) { |
| 1481 | + } else if (0==strcmp("efiruntime", o.arg)) { |
| 1483 | 1482 | s->pe_subsystem = 12; |
| 1484 | | − } else if (link_arg("efirom", p)) { |
| 1483 | + } else if (0==strcmp("efirom", o.arg)) { |
| 1485 | 1484 | s->pe_subsystem = 13; |
| 1486 | 1485 | #elif defined(TCC_TARGET_ARM) |
| 1487 | | − if (link_arg("wince", p)) { |
| 1486 | + if (0==strcmp("wince", o.arg)) { |
| 1488 | 1487 | s->pe_subsystem = 9; |
| 1489 | 1488 | #endif |
| 1490 | 1489 | } else |
| 1491 | 1490 | goto err; |
| 1492 | | −#endif |
| 1491 | +#endif /* PE */ |
| 1493 | 1492 | #ifdef TCC_TARGET_MACHO |
| 1494 | | − } else if (link_option(option, "all_load", &p)) { |
| 1495 | | − s->filetype |= AFF_WHOLE_ARCHIVE; |
| 1496 | | − } else if (link_option(option, "force_load", &p)) { |
| 1493 | + } else if (link_option(&o, "all_load")) { |
| 1497 | 1494 | s->filetype |= AFF_WHOLE_ARCHIVE; |
| 1498 | | − args_parser_add_file(s, p, AFF_TYPE_LIB | (s->filetype & ~AFF_TYPE_MASK)); |
| 1499 | | − s->nb_libraries++; |
| 1500 | | − } else if (link_option(option, "single_module", &p)) { |
| 1495 | + } else if (link_option(&o, "force_load=")) { |
| 1496 | + args_parser_add_file(s, o.arg, AFF_TYPE_LIB | AFF_WHOLE_ARCHIVE); |
| 1497 | + } else if (link_option(&o, "single_module")) { |
| 1501 | 1498 | ignoring = 1; |
| 1502 | 1499 | #endif |
| 1503 | | − } else if (ret = link_option(option, "?whole-archive", &p), ret) { |
| 1504 | | − if (ret > 0) |
| 1505 | | − s->filetype |= AFF_WHOLE_ARCHIVE; |
| 1506 | | − else |
| 1507 | | − s->filetype &= ~AFF_WHOLE_ARCHIVE; |
| 1508 | | − } else if (link_option(option, "z=", &p)) { |
| 1500 | + } else if (link_option(&o, "as-needed")) { |
| 1509 | 1501 | ignoring = 1; |
| 1510 | | − } else if (p) { |
| 1511 | | − return 0; |
| 1502 | + } else if (link_option(&o, "O")) { |
| 1503 | + ignoring = 1; |
| 1504 | + } else if (link_option(&o, "z=")) { |
| 1505 | + ignoring = 1; |
| 1506 | + } else if (link_option(&o, "L:")) { |
| 1507 | + tcc_add_library_path(s, o.arg); |
| 1508 | + } else if (link_option(&o, "l:")) { |
| 1509 | + args_parser_add_file(s, o.arg, AFF_TYPE_LIB | (s->filetype & ~AFF_TYPE_MASK)); |
| 1510 | + } else if (o.match) { |
| 1511 | + return 0; /* expecting argument with next '-Wl,' */ |
| 1512 | 1512 | } else { |
| 1513 | 1513 | err: |
| 1514 | | − return tcc_error_noabort("unsupported linker option '%s'", option); |
| 1514 | + return tcc_error_noabort("unsupported linker option '%s'", o.opt); |
| 1515 | 1515 | } |
| 1516 | 1516 | if (ignoring) |
| 1517 | | − tcc_warning_c(warn_unsupported)("unsupported linker option '%s'", option); |
| 1518 | | − option = skip_linker_arg(&p); |
| 1517 | + tcc_warning_c(warn_unsupported)("unsupported linker option '%s'", o.opt); |
| 1518 | + ++s->link_optind; |
| 1519 | 1519 | } |
| 1520 | | − return 1; |
| 1520 | + return 0; |
| 1521 | 1521 | } |
| 1522 | 1522 | |
| 1523 | 1523 | typedef struct TCCOption { |
| @@ -1541,7 +1541,6 @@ enum { · 17 unmodified lines |
| 1541 | 1541 | TCC_OPTION_bench, |
| 1542 | 1542 | TCC_OPTION_bt, |
| 1543 | 1543 | TCC_OPTION_b, |
| 1544 | | − TCC_OPTION_ba, |
| 1545 | 1544 | TCC_OPTION_g, |
| 1546 | 1545 | TCC_OPTION_c, |
| 1547 | 1546 | TCC_OPTION_dumpmachine, |
| @@ -1580,6 +1579,7 @@ enum { · 32 unmodified lines |
| 1580 | 1579 | TCC_OPTION_x, |
| 1581 | 1580 | TCC_OPTION_ar, |
| 1582 | 1581 | TCC_OPTION_impdef, |
| 1582 | + /* macho */ |
| 1583 | 1583 | TCC_OPTION_dynamiclib, |
| 1584 | 1584 | TCC_OPTION_flat_namespace, |
| 1585 | 1585 | TCC_OPTION_two_levelnamespace, |
| @@ -1617,11 +1617,13 @@ static const TCCOption tcc_options[] = { · 31 unmodified lines |
| 1617 | 1617 | #ifdef TCC_TARGET_MACHO |
| 1618 | 1618 | { "compatibility_version", TCC_OPTION_compatibility_version, TCC_OPTION_HAS_ARG }, |
| 1619 | 1619 | { "current_version", TCC_OPTION_current_version, TCC_OPTION_HAS_ARG }, |
| 1620 | | −#endif |
| 1621 | | − { "c", TCC_OPTION_c, 0 }, |
| 1622 | | −#ifdef TCC_TARGET_MACHO |
| 1623 | 1620 | { "dynamiclib", TCC_OPTION_dynamiclib, 0 }, |
| 1621 | + { "flat_namespace", TCC_OPTION_flat_namespace, 0 }, |
| 1622 | + { "install_name", TCC_OPTION_install_name, TCC_OPTION_HAS_ARG }, |
| 1623 | + { "two_levelnamespace", TCC_OPTION_two_levelnamespace, 0 }, |
| 1624 | + { "undefined", TCC_OPTION_undefined, TCC_OPTION_HAS_ARG }, |
| 1624 | 1625 | #endif |
| 1626 | + { "c", TCC_OPTION_c, 0 }, |
| 1625 | 1627 | { "dumpmachine", TCC_OPTION_dumpmachine, 0}, |
| 1626 | 1628 | { "dumpversion", TCC_OPTION_dumpversion, 0}, |
| 1627 | 1629 | { "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, |
| @@ -1642,9 +1644,6 @@ static const TCCOption tcc_options[] = { · 14 unmodified lines |
| 1642 | 1644 | { "mfloat-abi", TCC_OPTION_mfloat_abi, TCC_OPTION_HAS_ARG }, |
| 1643 | 1645 | #endif |
| 1644 | 1646 | { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, |
| 1645 | | −#ifdef TCC_TARGET_MACHO |
| 1646 | | − { "flat_namespace", TCC_OPTION_flat_namespace, 0 }, |
| 1647 | | −#endif |
| 1648 | 1647 | { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, |
| 1649 | 1648 | { "isystem", TCC_OPTION_isystem, TCC_OPTION_HAS_ARG }, |
| 1650 | 1649 | { "include", TCC_OPTION_include, TCC_OPTION_HAS_ARG }, |
| @@ -1660,14 +1659,10 @@ static const TCCOption tcc_options[] = { · 9 unmodified lines |
| 1660 | 1659 | { "MMD", TCC_OPTION_MMD, 0}, |
| 1661 | 1660 | { "MP", TCC_OPTION_MP, 0}, |
| 1662 | 1661 | { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG }, |
| 1662 | + /* tcctools */ |
| 1663 | 1663 | { "ar", TCC_OPTION_ar, 0}, |
| 1664 | 1664 | #ifdef TCC_TARGET_PE |
| 1665 | 1665 | { "impdef", TCC_OPTION_impdef, 0}, |
| 1666 | | −#endif |
| 1667 | | −#ifdef TCC_TARGET_MACHO |
| 1668 | | − { "install_name", TCC_OPTION_install_name, TCC_OPTION_HAS_ARG }, |
| 1669 | | − { "two_levelnamespace", TCC_OPTION_two_levelnamespace, 0 }, |
| 1670 | | − { "undefined", TCC_OPTION_undefined, TCC_OPTION_HAS_ARG }, |
| 1671 | 1666 | #endif |
| 1672 | 1667 | /* ignored (silently, except after -Wunsupported) */ |
| 1673 | 1668 | { "arch", 0, TCC_OPTION_HAS_ARG}, |
| @@ -1788,65 +1783,6 @@ static const char dumpmachine_str[] = · 114 unmodified lines |
| 1788 | 1783 | #endif |
| 1789 | 1784 | ; |
| 1790 | 1785 | |
| 1791 | | −static int args_parser_make_argv(const char *r, int *argc, char ***argv) |
| 1792 | | −{ |
| 1793 | | − int ret = 0, q, c; |
| 1794 | | − CString str; |
| 1795 | | − for(;;) { |
| 1796 | | − while (c = (unsigned char)*r, c && c <= ' ') |
| 1797 | | − ++r; |
| 1798 | | − if (c == 0) |
| 1799 | | − break; |
| 1800 | | − q = 0; |
| 1801 | | − cstr_new(&str); |
| 1802 | | − while (c = (unsigned char)*r, c) { |
| 1803 | | − ++r; |
| 1804 | | − if (c == '\\' && (*r == '"' || *r == '\\')) { |
| 1805 | | − c = *r++; |
| 1806 | | − } else if (c == '"') { |
| 1807 | | − q = !q; |
| 1808 | | − continue; |
| 1809 | | − } else if (q == 0 && c <= ' ') { |
| 1810 | | − break; |
| 1811 | | − } |
| 1812 | | − cstr_ccat(&str, c); |
| 1813 | | − } |
| 1814 | | − cstr_ccat(&str, 0); |
| 1815 | | − //printf("<%s>\n", str.data), fflush(stdout); |
| 1816 | | − dynarray_add(argv, argc, tcc_strdup(str.data)); |
| 1817 | | − cstr_free(&str); |
| 1818 | | − ++ret; |
| 1819 | | − } |
| 1820 | | − return ret; |
| 1821 | | −} |
| 1822 | | − |
| 1823 | | −/* read list file */ |
| 1824 | | −static int args_parser_listfile(TCCState *s, |
| 1825 | | − const char *filename, int optind, int *pargc, char ***pargv) |
| 1826 | | −{ |
| 1827 | | − TCCState *s1 = s; |
| 1828 | | − int fd, i; |
| 1829 | | − char *p; |
| 1830 | | − int argc = 0; |
| 1831 | | − char **argv = NULL; |
| 1832 | | − |
| 1833 | | − fd = open(filename, O_RDONLY | O_BINARY); |
| 1834 | | − if (fd < 0) |
| 1835 | | − return tcc_error_noabort("listfile '%s' not found", filename); |
| 1836 | | − |
| 1837 | | − p = tcc_load_text(fd); |
| 1838 | | − for (i = 0; i < *pargc; ++i) |
| 1839 | | − if (i == optind) |
| 1840 | | − args_parser_make_argv(p, &argc, &argv); |
| 1841 | | − else |
| 1842 | | − dynarray_add(&argv, &argc, tcc_strdup((*pargv)[i])); |
| 1843 | | − |
| 1844 | | − tcc_free(p); |
| 1845 | | − dynarray_reset(&s->argv, &s->argc); |
| 1846 | | − *pargc = s->argc = argc, *pargv = s->argv = argv; |
| 1847 | | − return 0; |
| 1848 | | −} |
| 1849 | | − |
| 1850 | 1786 | #if defined TCC_TARGET_MACHO |
| 1851 | 1787 | static uint32_t parse_version(TCCState *s1, const char *version) |
| 1852 | 1788 | { |
| @@ -1867,46 +1803,79 @@ static uint32_t parse_version(TCCState *s1, const char *version) · 14 unmodified lines |
| 1867 | 1803 | } |
| 1868 | 1804 | #endif |
| 1869 | 1805 | |
| 1870 | | −PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv, int optind) |
| 1806 | +/* insert args from 'p' (separated by sep or ' ') into argv at position 'optind' */ |
| 1807 | +static void insert_args(TCCState *s1, char ***pargv, int *pargc, int optind, const char *p, int sep) |
| 1808 | +{ |
| 1809 | + int argc = 0; |
| 1810 | + char **argv = NULL; |
| 1811 | + int i; |
| 1812 | + for (i = 0; i < *pargc; ++i) |
| 1813 | + if (i == optind) |
| 1814 | + dynarray_split(&argv, &argc, p, sep); |
| 1815 | + else |
| 1816 | + dynarray_add(&argv, &argc, tcc_strdup((*pargv)[i])); |
| 1817 | + dynarray_reset(&s1->argv, &s1->argc); |
| 1818 | + *pargc = s1->argc = argc; |
| 1819 | + *pargv = s1->argv = argv; |
| 1820 | +} |
| 1821 | + |
| 1822 | +static void args_parser_add_file(TCCState *s, const char* filename, int filetype) |
| 1823 | +{ |
| 1824 | + struct filespec *f = tcc_malloc(sizeof *f + strlen(filename)); |
| 1825 | + f->type = filetype; |
| 1826 | + strcpy(f->name, filename); |
| 1827 | + dynarray_add(&s->files, &s->nb_files, f); |
| 1828 | + if (filetype & AFF_TYPE_LIB) |
| 1829 | + ++s->nb_libraries; |
| 1830 | +} |
| 1831 | + |
| 1832 | +/* using * to argc/argv to let "tcc -ar" benefit from @listfile expansion */ |
| 1833 | +PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv) |
| 1871 | 1834 | { |
| 1872 | 1835 | TCCState *s1 = s; |
| 1873 | 1836 | const TCCOption *popt; |
| 1874 | 1837 | const char *optarg, *r; |
| 1875 | 1838 | const char *run = NULL; |
| 1876 | 1839 | int x; |
| 1877 | | − int tool = 0, arg_start = 0, noaction = optind; |
| 1840 | + int tool = 0, arg_start = 0, not_empty = 0, optind = 1; |
| 1878 | 1841 | char **argv = *pargv; |
| 1879 | 1842 | int argc = *pargc; |
| 1880 | 1843 | |
| 1881 | | − cstr_reset(&s->linker_arg); |
| 1844 | + s->link_optind = s->link_argc; |
| 1882 | 1845 | |
| 1883 | 1846 | while (optind < argc) { |
| 1884 | 1847 | r = argv[optind]; |
| 1885 | | − if (r[0] == '@' && r[1] != '\0') { |
| 1886 | | − if (args_parser_listfile(s, r + 1, optind, &argc, &argv)) |
| 1887 | | − return -1; |
| 1848 | + if (r[0] == '@' && r[1] != '\0') { /* read @listfile */ |
| 1849 | + int fd; char *p; |
| 1850 | + fd = open(++r, O_RDONLY | O_BINARY); |
| 1851 | + if (fd < 0) |
| 1852 | + return tcc_error_noabort("listfile '%s' not found", r); |
| 1853 | + p = tcc_load_text(fd); |
| 1854 | + insert_args(s1, &argv, &argc, optind, p, 0); |
| 1855 | + close(fd), tcc_free(p); |
| 1888 | 1856 | continue; |
| 1889 | 1857 | } |
| 1890 | 1858 | optind++; |
| 1891 | | − if (tool) { |
| 1892 | | − if (r[0] == '-' && r[1] == 'v' && r[2] == 0) |
| 1893 | | − ++s->verbose; |
| 1859 | + if (tool) { /* ignore all except -v and @listfile */ |
| 1860 | + s->verbose += !strcmp(r, "-v"); |
| 1894 | 1861 | continue; |
| 1895 | 1862 | } |
| 1896 | | − if (r[0] != '-' || r[1] == '\0') { |
| 1863 | + |
| 1864 | + if (r[0] != '-' || r[1] == '\0') { /* file or '-' (stdin) */ |
| 1897 | 1865 | args_parser_add_file(s, r, s->filetype); |
| 1866 | + not_empty = 1; |
| 1867 | + dorun: |
| 1898 | 1868 | if (run) { |
| 1899 | | −dorun: |
| 1869 | + /* tcc -run <file> <args...> */ |
| 1900 | 1870 | if (tcc_set_options(s, run)) |
| 1901 | 1871 | return -1; |
| 1902 | | − arg_start = optind - 1; |
| 1872 | + arg_start = optind - 1; /* argv[0] will be <file> */ |
| 1903 | 1873 | break; |
| 1904 | 1874 | } |
| 1905 | 1875 | continue; |
| 1906 | 1876 | } |
| 1907 | | − |
| 1908 | | − /* allow "tcc files... -run -- args ..." */ |
| 1909 | | − if (r[1] == '-' && r[2] == '\0' && run) |
| 1877 | + /* Also allow "tcc <files...> -run -- <args...>" */ |
| 1878 | + if (r[1] == '-' && r[2] == '\0') |
| 1910 | 1879 | goto dorun; |
| 1911 | 1880 | |
| 1912 | 1881 | /* find option in table */ |
| @@ -1921,7 +1890,6 @@ dorun: · 8 unmodified lines |
| 1921 | 1890 | if (popt->flags & TCC_OPTION_HAS_ARG) { |
| 1922 | 1891 | if (*r1 == '\0' && !(popt->flags & TCC_OPTION_NOSEP)) { |
| 1923 | 1892 | if (optind >= argc) |
| 1924 | | − arg_err: |
| 1925 | 1893 | return tcc_error_noabort("argument to '%s' is missing", r); |
| 1926 | 1894 | optarg = argv[optind++]; |
| 1927 | 1895 | } |
| @@ -1931,12 +1899,6 @@ dorun: · 3 unmodified lines |
| 1931 | 1899 | } |
| 1932 | 1900 | |
| 1933 | 1901 | switch(popt->index) { |
| 1934 | | − case TCC_OPTION_HELP: |
| 1935 | | − x = OPT_HELP; |
| 1936 | | − goto extra_action; |
| 1937 | | − case TCC_OPTION_HELP2: |
| 1938 | | − x = OPT_HELP2; |
| 1939 | | − goto extra_action; |
| 1940 | 1902 | case TCC_OPTION_I: |
| 1941 | 1903 | tcc_add_include_path(s, optarg); |
| 1942 | 1904 | break; |
| @@ -1952,11 +1914,9 @@ dorun: · 9 unmodified lines |
| 1952 | 1914 | case TCC_OPTION_B: |
| 1953 | 1915 | /* set tcc utilities path (mainly for tcc development) */ |
| 1954 | 1916 | tcc_set_lib_path(s, optarg); |
| 1955 | | − ++noaction; |
| 1956 | | − break; |
| 1917 | + continue; |
| 1957 | 1918 | case TCC_OPTION_l: |
| 1958 | 1919 | args_parser_add_file(s, optarg, AFF_TYPE_LIB | (s->filetype & ~AFF_TYPE_MASK)); |
| 1959 | | − s->nb_libraries++; |
| 1960 | 1920 | break; |
| 1961 | 1921 | case TCC_OPTION_pthread: |
| 1962 | 1922 | s->option_pthread = 1; |
| @@ -2027,14 +1987,14 @@ dorun: · 64 unmodified lines |
| 2027 | 1987 | x = TCC_OUTPUT_DLL; |
| 2028 | 1988 | goto set_output_type; |
| 2029 | 1989 | case TCC_OPTION_soname: |
| 2030 | | − s->soname = tcc_strdup(optarg); |
| 1990 | + tcc_set_str(&s->soname, optarg); |
| 2031 | 1991 | break; |
| 2032 | 1992 | case TCC_OPTION_o: |
| 2033 | 1993 | if (s->outfile) { |
| 2034 | 1994 | tcc_warning("multiple -o option"); |
| 2035 | 1995 | tcc_free(s->outfile); |
| 2036 | 1996 | } |
| 2037 | | − s->outfile = tcc_strdup(optarg); |
| 1997 | + tcc_set_str(&s->outfile, optarg); |
| 2038 | 1998 | break; |
| 2039 | 1999 | case TCC_OPTION_r: |
| 2040 | 2000 | /* generate a .o merging several output files */ |
| @@ -2054,17 +2014,18 @@ dorun: · 13 unmodified lines |
| 2054 | 2014 | s->nostdlib = 1; |
| 2055 | 2015 | break; |
| 2056 | 2016 | case TCC_OPTION_run: |
| 2057 | | −#ifndef TCC_IS_NATIVE |
| 2058 | | − return tcc_error_noabort("-run is not available in a cross compiler"); |
| 2059 | | −#else |
| 2017 | +#ifdef TCC_IS_NATIVE |
| 2018 | + /* When from script "#!/usr/bin/tcc -run <options>", |
| 2019 | + argv[1] is "-run <options>" and argv[2] is <script-name> */ |
| 2060 | 2020 | run = optarg; |
| 2061 | 2021 | x = TCC_OUTPUT_MEMORY; |
| 2062 | 2022 | goto set_output_type; |
| 2023 | +#else |
| 2024 | + return tcc_error_noabort("-run is not available in a cross compiler"); |
| 2063 | 2025 | #endif |
| 2064 | 2026 | case TCC_OPTION_v: |
| 2065 | 2027 | do ++s->verbose; while (*optarg++ == 'v'); |
| 2066 | | − ++noaction; |
| 2067 | | − break; |
| 2028 | + continue; |
| 2068 | 2029 | case TCC_OPTION_f: |
| 2069 | 2030 | if (set_flag(s, options_f, optarg) < 0) |
| 2070 | 2031 | goto unsupported_option; |
| @@ -2086,7 +2047,7 @@ dorun: · 15 unmodified lines |
| 2086 | 2047 | goto unsupported_option; |
| 2087 | 2048 | if (PTR_SIZE != x/8) |
| 2088 | 2049 | return x; |
| 2089 | | − ++noaction; |
| 2050 | + continue; |
| 2090 | 2051 | } |
| 2091 | 2052 | break; |
| 2092 | 2053 | case TCC_OPTION_W: |
| @@ -2101,25 +2062,13 @@ dorun: · 8 unmodified lines |
| 2101 | 2062 | s->rdynamic = 1; |
| 2102 | 2063 | break; |
| 2103 | 2064 | case TCC_OPTION_Wl: |
| 2104 | | − if (s->linker_arg.size) |
| 2105 | | − ((char*)s->linker_arg.data)[s->linker_arg.size - 1] = ','; |
| 2106 | | − cstr_cat(&s->linker_arg, optarg, 0); |
| 2107 | | − x = tcc_set_linker(s, s->linker_arg.data); |
| 2108 | | − if (x) |
| 2109 | | − cstr_reset(&s->linker_arg); |
| 2110 | | − if (x < 0) |
| 2065 | + if (tcc_set_linker(s, optarg) < 0) |
| 2111 | 2066 | return -1; |
| 2112 | 2067 | break; |
| 2113 | 2068 | case TCC_OPTION_Wp: |
| 2114 | | − { |
| 2115 | | − char *p = tcc_strdup(optarg), *q = p; |
| 2116 | | − while (!!(q = strchr(q, ','))) *q++ = ' '; |
| 2117 | | − x = tcc_set_options(s, p); |
| 2118 | | − tcc_free(p); |
| 2119 | | − if (x < 0) |
| 2120 | | − return -1; |
| 2069 | + if (argv[0]) /* not with tcc_set_options() */ |
| 2070 | + insert_args(s, &argv, &argc, --optind, optarg, ','); |
| 2121 | 2071 | break; |
| 2122 | | − } |
| 2123 | 2072 | case TCC_OPTION_E: |
| 2124 | 2073 | x = TCC_OUTPUT_PREPROCESS; |
| 2125 | 2074 | goto set_output_type; |
| @@ -2132,7 +2081,7 @@ dorun: · 6 unmodified lines |
| 2132 | 2081 | case TCC_OPTION_MM: |
| 2133 | 2082 | s->just_deps = 1; |
| 2134 | 2083 | if(!s->deps_outfile) |
| 2135 | | − s->deps_outfile = tcc_strdup("-"); |
| 2084 | + tcc_set_str(&s->deps_outfile, "-"); |
| 2136 | 2085 | // fall through |
| 2137 | 2086 | case TCC_OPTION_MMD: |
| 2138 | 2087 | s->gen_deps = 1; |
| @@ -2142,7 +2091,7 @@ dorun: · 3 unmodified lines |
| 2142 | 2091 | s->include_sys_deps = 1; |
| 2143 | 2092 | break; |
| 2144 | 2093 | case TCC_OPTION_MF: |
| 2145 | | − s->deps_outfile = tcc_strdup(optarg); |
| 2094 | + tcc_set_str(&s->deps_outfile, optarg); |
| 2146 | 2095 | break; |
| 2147 | 2096 | case TCC_OPTION_MP: |
| 2148 | 2097 | s->gen_phony_deps = 1; |
| @@ -2170,12 +2119,6 @@ dorun: · 21 unmodified lines |
| 2170 | 2119 | case TCC_OPTION_O: |
| 2171 | 2120 | s->optimize = isnum(optarg[0]) ? optarg[0]-'0' : 1 /* -O -Os */; |
| 2172 | 2121 | break; |
| 2173 | | − case TCC_OPTION_print_search_dirs: |
| 2174 | | − x = OPT_PRINT_DIRS; |
| 2175 | | − goto extra_action; |
| 2176 | | − case TCC_OPTION_impdef: |
| 2177 | | − x = OPT_IMPDEF; |
| 2178 | | − goto extra_action; |
| 2179 | 2122 | #if defined TCC_TARGET_MACHO |
| 2180 | 2123 | case TCC_OPTION_dynamiclib: |
| 2181 | 2124 | x = TCC_OUTPUT_DLL; |
| @@ -2187,7 +2130,7 @@ dorun: · 5 unmodified lines |
| 2187 | 2130 | case TCC_OPTION_undefined: |
| 2188 | 2131 | break; |
| 2189 | 2132 | case TCC_OPTION_install_name: |
| 2190 | | − s->install_name = tcc_strdup(optarg); |
| 2133 | + tcc_set_str(&s->install_name, optarg); |
| 2191 | 2134 | break; |
| 2192 | 2135 | case TCC_OPTION_compatibility_version: |
| 2193 | 2136 | s->compatibility_version = parse_version(s, optarg); |
| @@ -2196,11 +2139,23 @@ dorun: · 2 unmodified lines |
| 2196 | 2139 | s->current_version = parse_version(s, optarg);; |
| 2197 | 2140 | break; |
| 2198 | 2141 | #endif |
| 2142 | + case TCC_OPTION_HELP: |
| 2143 | + x = OPT_HELP; |
| 2144 | + goto extra_action; |
| 2145 | + case TCC_OPTION_HELP2: |
| 2146 | + x = OPT_HELP2; |
| 2147 | + goto extra_action; |
| 2148 | + case TCC_OPTION_print_search_dirs: |
| 2149 | + x = OPT_PRINT_DIRS; |
| 2150 | + goto extra_action; |
| 2151 | + case TCC_OPTION_impdef: |
| 2152 | + x = OPT_IMPDEF; |
| 2153 | + goto extra_action; |
| 2199 | 2154 | case TCC_OPTION_ar: |
| 2200 | 2155 | x = OPT_AR; |
| 2201 | 2156 | extra_action: |
| 2202 | 2157 | arg_start = optind - 1; |
| 2203 | | − if (arg_start != noaction) |
| 2158 | + if (not_empty) |
| 2204 | 2159 | return tcc_error_noabort("cannot parse %s here", r); |
| 2205 | 2160 | tool = x; |
| 2206 | 2161 | break; |
| @@ -2209,16 +2164,19 @@ unsupported_option: · 2 unmodified lines |
| 2209 | 2164 | tcc_warning_c(warn_unsupported)("unsupported option '%s'", r); |
| 2210 | 2165 | break; |
| 2211 | 2166 | } |
| 2167 | + not_empty = 1; |
| 2212 | 2168 | } |
| 2213 | | − if (s->linker_arg.size) { |
| 2214 | | − r = s->linker_arg.data; |
| 2215 | | − goto arg_err; |
| 2216 | | − } |
| 2217 | | − *pargc = argc - arg_start; |
| 2218 | | − *pargv = argv + arg_start; |
| 2219 | | − if (tool) |
| 2169 | + |
| 2170 | + if (s->link_optind < s->link_argc) |
| 2171 | + return tcc_error_noabort("argument to '-Wl,%s' is missing", s->link_argv[s->link_optind]); |
| 2172 | + if (NULL == argv[0]) /* from tcc_set_options() */ |
| 2173 | + return 0; |
| 2174 | + if (arg_start) { |
| 2175 | + *pargc = argc - arg_start; |
| 2176 | + *pargv = argv + arg_start; |
| 2220 | 2177 | return tool; |
| 2221 | | − if (optind != noaction) |
| 2178 | + } |
| 2179 | + if (not_empty) |
| 2222 | 2180 | return 0; |
| 2223 | 2181 | if (s->verbose == 2) |
| 2224 | 2182 | return OPT_PRINT_DIRS; |
| @@ -2231,10 +2189,11 @@ LIBTCCAPI int tcc_set_options(TCCState *s, const char *r) · 6 unmodified lines |
| 2231 | 2189 | { |
| 2232 | 2190 | char **argv = NULL; |
| 2233 | 2191 | int argc = 0, ret; |
| 2234 | | − args_parser_make_argv(r, &argc, &argv); |
| 2235 | | − ret = tcc_parse_args(s, &argc, &argv, 0); |
| 2192 | + dynarray_add(&argv, &argc, 0); |
| 2193 | + dynarray_split(&argv, &argc, r, 0); |
| 2194 | + ret = tcc_parse_args(s, &argc, &argv); |
| 2236 | 2195 | dynarray_reset(&argv, &argc); |
| 2237 | | − return ret < 0 ? ret : 0; |
| 2196 | + return ret; |
| 2238 | 2197 | } |
| 2239 | 2198 | |
| 2240 | 2199 | PUB_FUNC void tcc_print_stats(TCCState *s1, unsigned total_time) |
| @@ -2267,9 +2226,3 @@ PUB_FUNC void tcc_print_stats(TCCState *s1, unsigned total_time) · 26 unmodified lines |
| 2267 | 2226 | fprintf(stderr, " %d max (bytes)\n", mem_max_size); |
| 2268 | 2227 | #endif |
| 2269 | 2228 | } |
| 2270 | | − |
| 2271 | | −#if ONE_SOURCE |
| 2272 | | −# undef malloc |
| 2273 | | −# undef realloc |
| 2274 | | −# undef free |
| 2275 | | −#endif |