support -l, -L to set file path of dynamic lib*.so master
authoryu.dongliang <18588496441@163.com>
Mon, 27 Jul 2026 09:20:34 +0000 (17:20 +0800)
committeryu.dongliang <18588496441@163.com>
Mon, 27 Jul 2026 09:20:34 +0000 (17:20 +0800)
parse/main.c

index 32371fa9e50016f05bf46cb207798292c63e7729..e72b0b88a357136962797e8f6ee64793f0f5c5b6 100644 (file)
@@ -93,6 +93,76 @@ int add_sys_files(scf_vector_t* vec, const char* sysroot, const char* arch, char
        return 0;
 }
 
+static int _lib_file_cmp(const void* v0, const void* v1)
+{
+       return strcmp(v0, v1);
+}
+
+int add_so_files(scf_vector_t* vec, const scf_vector_t* lib_paths, const scf_vector_t* lib_files)
+{
+       int i;
+       for (i = 0; i  < lib_files->size; i++) {
+               char* file = lib_files->data[i];
+
+               size_t len = strlen(file);
+
+               if (1 == len && 'c' == file[0]) // ignore libc.so, default libc.so.6 already added.
+                       continue;
+
+               scf_string_t* s = scf_string_cstr_len("lib", 3);
+               if (!s)
+                       return -ENOMEM;
+
+               int ret = scf_string_cat_cstr_len(s, file, len);
+               if (ret < 0) {
+                       scf_string_free(s);
+                       return ret;
+               }
+
+               ret = scf_string_cat_cstr(s, ".so");
+               if (ret < 0) {
+                       scf_string_free(s);
+                       return ret;
+               }
+
+               scf_logi("s->data: %s\n", s->data);
+
+               uint8_t* path = NULL;
+
+               ret = scf_file_find(&path, lib_paths, s);
+               if (ret < 0) {
+                       scf_string_free(s);
+                       return ret;
+               }
+
+               if (!path) {
+                       path = s->data;
+
+                       s->data = NULL;
+                       s->len  = 0;
+                       s->capacity = -1;
+               }
+
+               scf_string_free(s);
+               s = NULL;
+
+               if (!scf_vector_find_cmp(vec, path, _lib_file_cmp)) {
+
+                       scf_logi("path: %s\n", path);
+
+                       ret = scf_vector_add(vec, path);
+                       if (ret < 0) {
+                               free(path);
+                               return ret;
+                       }
+               } else
+                       free(path);
+               path = NULL;
+       }
+
+       return 0;
+}
+
 int main(int argc, char* argv[])
 {
        if (argc < 2) {
@@ -318,7 +388,14 @@ int main(int argc, char* argv[])
                parse = NULL;
        }
 
+       int obj_size = objs->size;
+       int so_size  = sofiles->size;
+
        if (link) {
+               ret = add_so_files(sofiles, lib_paths, lib_files);
+               if (ret < 0)
+                       goto error;
+
 #define MAIN_ADD_FILES(_objs, _sofiles, _arch) \
                do { \
                        if (!dyn) { \
@@ -371,14 +448,30 @@ error:
        if (afiles)
                scf_vector_free(afiles);
 
-       if (sofiles)
-               scf_vector_free(sofiles);
-
        if (srcs)
                scf_vector_free(srcs);
 
-       if (objs)
+       if (sofiles) {
+               for (i = so_size; i < sofiles->size; i++) {
+                       char* file      = sofiles->data[i];
+
+                       scf_logd("i: %d, file: %p, %s\n", i, file, file);
+
+                       free(file);
+               }
+
+               scf_vector_free(sofiles);
+       }
+
+       if (objs) {
+               for (i = obj_size; i < objs->size - 1; i++) {
+                       char* file       = objs->data[i];
+
+                       free(file);
+               }
+
                scf_vector_free(objs);
+       }
 
        return ret;
 }