trylink: gold may not support --sort-common (yet)
[oweals/busybox.git] / modutils / modutils-24.c
index 9f91c9979883e719d2bf0a04180a288c634a3fcb..5c1611c293050ab4808a4573c034a68812117354 100644 (file)
@@ -865,19 +865,23 @@ arch_apply_relocation(struct obj_file *f,
                        break;
                case R_H8_PCREL16:
                        v -= dot + 2;
-                       if ((ElfW(Sword))v > 0x7fff ||
-                           (ElfW(Sword))v < -(ElfW(Sword))0x8000)
+                       if ((ElfW(Sword))v > 0x7fff
+                        || (ElfW(Sword))v < -(ElfW(Sword))0x8000
+                       ) {
                                ret = obj_reloc_overflow;
-                       else
+                       } else {
                                *(unsigned short *)loc = v;
+                       }
                        break;
                case R_H8_PCREL8:
                        v -= dot + 1;
-                       if ((ElfW(Sword))v > 0x7f ||
-                           (ElfW(Sword))v < -(ElfW(Sword))0x80)
+                       if ((ElfW(Sword))v > 0x7f
+                        || (ElfW(Sword))v < -(ElfW(Sword))0x80
+                       ) {
                                ret = obj_reloc_overflow;
-                       else
+                       } else {
                                *(unsigned char *)loc = v;
+                       }
                        break;
 
 #elif defined(__i386__)
@@ -1566,7 +1570,7 @@ arch_apply_relocation(struct obj_file *f,
 #endif
 
                default:
-                       printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info));
+                       printf("Warning: unhandled reloc %d\n", (int)ELF_R_TYPE(rel->r_info));
                        ret = obj_reloc_unhandled;
                        break;
 
@@ -2432,11 +2436,11 @@ new_process_module_arguments(struct obj_file *f, const char *options)
                loc = contents + sym->value;
 
                if (*pinfo == 'c') {
-                       if (!isdigit(*(pinfo + 1))) {
+                       if (!isdigit(pinfo[1])) {
                                bb_error_msg_and_die("parameter type 'c' for %s must be followed by"
                                                     " the maximum size", param);
                        }
-                       charssize = strtoul(pinfo + 1, (char **) NULL, 10);
+                       charssize = strtoul(pinfo + 1, NULL, 10);
                }
 
                if (val == NULL) {
@@ -2449,6 +2453,8 @@ new_process_module_arguments(struct obj_file *f, const char *options)
                n = 0;
                p = val;
                while (*p != 0) {
+                       char *endp;
+
                        if (++n > max)
                                bb_error_msg_and_die("too many values for %s (max %d)", param, max);
 
@@ -2472,19 +2478,23 @@ new_process_module_arguments(struct obj_file *f, const char *options)
                                p += len;
                                break;
                        case 'b':
-                               *loc++ = strtoul(p, &p, 0);
+                               *loc++ = strtoul(p, &endp, 0);
+                               p = endp; /* gcc likes temp var for &endp */
                                break;
                        case 'h':
-                               *(short *) loc = strtoul(p, &p, 0);
+                               *(short *) loc = strtoul(p, &endp, 0);
                                loc += tgt_sizeof_short;
+                               p = endp;
                                break;
                        case 'i':
-                               *(int *) loc = strtoul(p, &p, 0);
+                               *(int *) loc = strtoul(p, &endp, 0);
                                loc += tgt_sizeof_int;
+                               p = endp;
                                break;
                        case 'l':
-                               *(long *) loc = strtoul(p, &p, 0);
+                               *(long *) loc = strtoul(p, &endp, 0);
                                loc += tgt_sizeof_long;
+                               p = endp;
                                break;
                        default:
                                bb_error_msg_and_die("unknown parameter type '%c' for %s",
@@ -3193,6 +3203,7 @@ static int obj_create_image(struct obj_file *f, char *image)
 
 static struct obj_file *obj_load(char *image, size_t image_size, int loadprogbits)
 {
+       typedef uint32_t aliased_uint32_t FIX_ALIASING;
 #if BB_LITTLE_ENDIAN
 # define ELFMAG_U32 ((uint32_t)(ELFMAG0 + 0x100 * (ELFMAG1 + (0x100 * (ELFMAG2 + 0x100 * ELFMAG3)))))
 #else
@@ -3214,7 +3225,7 @@ static struct obj_file *obj_load(char *image, size_t image_size, int loadprogbit
                bb_error_msg_and_die("error while loading ELF header");
        memcpy(&f->header, image, sizeof(f->header));
 
-       if (*(uint32_t*)(&f->header.e_ident) != ELFMAG_U32) {
+       if (*(aliased_uint32_t*)(&f->header.e_ident) != ELFMAG_U32) {
                bb_error_msg_and_die("not an ELF file");
        }
        if (f->header.e_ident[EI_CLASS] != ELFCLASSM
@@ -3274,6 +3285,9 @@ static struct obj_file *obj_load(char *image, size_t image_size, int loadprogbit
                        case SHT_SYMTAB:
                        case SHT_STRTAB:
                        case SHT_RELM:
+#if defined(__mips__)
+                       case SHT_MIPS_DWARF:
+#endif
                                sec->contents = NULL;
                                if (sec->header.sh_size > 0) {
                                        sec->contents = xmalloc(sec->header.sh_size);
@@ -3777,12 +3791,20 @@ int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options)
        int m_has_modinfo;
 #endif
        char *image;
-       size_t image_size = 64 * 1024 * 1024;
-
-       /* Load module into memory and unzip if compressed */
-       image = xmalloc_open_zipped_read_close(m_filename, &image_size);
-       if (!image)
-               return EXIT_FAILURE;
+       size_t image_size;
+       bool mmaped;
+
+       image_size = INT_MAX - 4095;
+       mmaped = 0;
+       image = try_to_mmap_module(m_filename, &image_size);
+       if (image) {
+               mmaped = 1;
+       } else {
+               /* Load module into memory and unzip if compressed */
+               image = xmalloc_open_zipped_read_close(m_filename, &image_size);
+               if (!image)
+                       return EXIT_FAILURE;
+       }
 
        m_name = xstrdup(bb_basename(m_filename));
        /* "module.o[.gz]" -> "module" */
@@ -3895,7 +3917,10 @@ int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options)
        exit_status = EXIT_SUCCESS;
 
  out:
-       free(image);
+       if (mmaped)
+               munmap(image, image_size);
+       else
+               free(image);
        free(m_name);
 
        return exit_status;