linux/scripts/sortextable.c
<<
>>
Prefs
   1/*
   2 * sortextable.c: Sort the kernel's exception table
   3 *
   4 * Copyright 2011 - 2012 Cavium, Inc.
   5 *
   6 * Based on code taken from recortmcount.c which is:
   7 *
   8 * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>.  All rights reserved.
   9 * Licensed under the GNU General Public License, version 2 (GPLv2).
  10 *
  11 * Restructured to fit Linux format, as well as other updates:
  12 *  Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
  13 */
  14
  15/*
  16 * Strategy: alter the vmlinux file in-place.
  17 */
  18
  19#include <sys/types.h>
  20#include <sys/mman.h>
  21#include <sys/stat.h>
  22#include <getopt.h>
  23#include <elf.h>
  24#include <fcntl.h>
  25#include <setjmp.h>
  26#include <stdio.h>
  27#include <stdlib.h>
  28#include <string.h>
  29#include <unistd.h>
  30
  31#include <tools/be_byteshift.h>
  32#include <tools/le_byteshift.h>
  33
  34#ifndef EM_AARCH64
  35#define EM_AARCH64      183
  36#endif
  37
  38static int fd_map;      /* File descriptor for file being modified. */
  39static int mmap_failed; /* Boolean flag. */
  40static void *ehdr_curr; /* current ElfXX_Ehdr *  for resource cleanup */
  41static struct stat sb;  /* Remember .st_size, etc. */
  42static jmp_buf jmpenv;  /* setjmp/longjmp per-file error escape */
  43
  44/* setjmp() return values */
  45enum {
  46        SJ_SETJMP = 0,  /* hardwired first return */
  47        SJ_FAIL,
  48        SJ_SUCCEED
  49};
  50
  51/* Per-file resource cleanup when multiple files. */
  52static void
  53cleanup(void)
  54{
  55        if (!mmap_failed)
  56                munmap(ehdr_curr, sb.st_size);
  57        close(fd_map);
  58}
  59
  60static void __attribute__((noreturn))
  61fail_file(void)
  62{
  63        cleanup();
  64        longjmp(jmpenv, SJ_FAIL);
  65}
  66
  67/*
  68 * Get the whole file as a programming convenience in order to avoid
  69 * malloc+lseek+read+free of many pieces.  If successful, then mmap
  70 * avoids copying unused pieces; else just read the whole file.
  71 * Open for both read and write.
  72 */
  73static void *mmap_file(char const *fname)
  74{
  75        void *addr;
  76
  77        fd_map = open(fname, O_RDWR);
  78        if (fd_map < 0 || fstat(fd_map, &sb) < 0) {
  79                perror(fname);
  80                fail_file();
  81        }
  82        if (!S_ISREG(sb.st_mode)) {
  83                fprintf(stderr, "not a regular file: %s\n", fname);
  84                fail_file();
  85        }
  86        addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED,
  87                    fd_map, 0);
  88        if (addr == MAP_FAILED) {
  89                mmap_failed = 1;
  90                fprintf(stderr, "Could not mmap file: %s\n", fname);
  91                fail_file();
  92        }
  93        return addr;
  94}
  95
  96static uint64_t r8be(const uint64_t *x)
  97{
  98        return get_unaligned_be64(x);
  99}
 100static uint32_t rbe(const uint32_t *x)
 101{
 102        return get_unaligned_be32(x);
 103}
 104static uint16_t r2be(const uint16_t *x)
 105{
 106        return get_unaligned_be16(x);
 107}
 108static uint64_t r8le(const uint64_t *x)
 109{
 110        return get_unaligned_le64(x);
 111}
 112static uint32_t rle(const uint32_t *x)
 113{
 114        return get_unaligned_le32(x);
 115}
 116static uint16_t r2le(const uint16_t *x)
 117{
 118        return get_unaligned_le16(x);
 119}
 120
 121static void w8be(uint64_t val, uint64_t *x)
 122{
 123        put_unaligned_be64(val, x);
 124}
 125static void wbe(uint32_t val, uint32_t *x)
 126{
 127        put_unaligned_be32(val, x);
 128}
 129static void w2be(uint16_t val, uint16_t *x)
 130{
 131        put_unaligned_be16(val, x);
 132}
 133static void w8le(uint64_t val, uint64_t *x)
 134{
 135        put_unaligned_le64(val, x);
 136}
 137static void wle(uint32_t val, uint32_t *x)
 138{
 139        put_unaligned_le32(val, x);
 140}
 141static void w2le(uint16_t val, uint16_t *x)
 142{
 143        put_unaligned_le16(val, x);
 144}
 145
 146static uint64_t (*r8)(const uint64_t *);
 147static uint32_t (*r)(const uint32_t *);
 148static uint16_t (*r2)(const uint16_t *);
 149static void (*w8)(uint64_t, uint64_t *);
 150static void (*w)(uint32_t, uint32_t *);
 151static void (*w2)(uint16_t, uint16_t *);
 152
 153typedef void (*table_sort_t)(char *, int);
 154
 155/* 32 bit and 64 bit are very similar */
 156#include "sortextable.h"
 157#define SORTEXTABLE_64
 158#include "sortextable.h"
 159
 160static int compare_relative_table(const void *a, const void *b)
 161{
 162        int32_t av = (int32_t)r(a);
 163        int32_t bv = (int32_t)r(b);
 164
 165        if (av < bv)
 166                return -1;
 167        if (av > bv)
 168                return 1;
 169        return 0;
 170}
 171
 172static void sort_relative_table(char *extab_image, int image_size)
 173{
 174        int i;
 175
 176        /*
 177         * Do the same thing the runtime sort does, first normalize to
 178         * being relative to the start of the section.
 179         */
 180        i = 0;
 181        while (i < image_size) {
 182                uint32_t *loc = (uint32_t *)(extab_image + i);
 183                w(r(loc)a href="+code=i" class="sref">i);
<, loci);
 184                i += 4ss="sref">i);
 185        }
 186
 187        qsort(extab_image, image_size / 8, 8, compare_relative_tableass="sref">i);
 188
 189        /* Now denormalize. */
 190        i = 0;
 191        while (i < image_size) {
 192                uint32_t *loc = (uint32_t *)(extab_image + i);
 193                w(r(loc)a-href="+code=i" class="sref">i);
<, loci);
 194                i += 4ss="sref">i);
 195        }
 196}
 197
 198static void
 199do_file(char const *const fname)
 200{
 201        table_sort_t custom_sort;
 202        Elf32_Ehdr *ehdr = mmap_file(fname);
 20/a>
 200uint0" class="liuint0" class="l-f="+code=mmap_file" class="sref">mmap_fil_t" class="sref">uint32_t<        1u1n/a>        mmap_fil_t" class="s-cripa href="+code=e"_idtext">mmap_fil_t" c_idtexs="s[*ehdr 192       2return default:line" name="L192"> 192       2rt;fd_map, 0);"scripts/sortext class="sref">stderr, "Could not mmap file: %s\n", fndref">fname);
 192       2rt;uin20able.c#L169" id="L169t32_t" class="sref">uint0" f">mmap_fil_t" class="s-cripa href="+code=e"_idtext">mmap_fil_t" c_idtexs="s[*ehdr  91          2f="script2/sortextable.c#L110" id=2L110"20"sref">mmap_failed = 1;
  92        }
2return fprintf(  92        }
2rt;      183
ehdr 192       2a href="+2ode=uint32_t" class="sre2">uin21ef">uint32_t *uint32_t  92        }
2rcripts/so/sortextable.c#L114" id=2L114"21a>(r< class="sref">uint16_t<=mmap_file" class="t" class="sref">uint16_t  92        }
2r"scripts/href="+code=get_unaligne2_le3221a> += 4ss="sref">i);
uint64_t<=mmap_file" class="t" class="sref">uint64_t  92        }
2rstom_sort/sortextable.c#L116" id=2L116"215> += 4ss="sref">i);
uint32_t   92        }
2return uin21">munmap(uint16_t, <=mmap_file" class=sss="sref">uint16_t   92        }
2rt;fd_map, 0);"scripts/sortex="sref">uint64_t, <=mmap_file" class=sss="sref">uint64_t   92        }
2rt;  92        }
2r="script2/sortextable.c#L120" id=2L120"219able_sort_t      183
ehdr 192       2lt;fprintf(uint32_t  22#include <t;w822ref">fail_file();
uint16_t<=mmap_file" class="t" class="sref">uint16_t  22#include <t;uin22ef">uint32_t *uint64_t<=mmap_file" class="t" class="sref">uint64_t  22#include <cripts/sof="include/elf.h" class="class22a>(r(uint32_t   22#include <"scripts//sortextable.c#L125" id=2L125"22a> += 4ss="sref">i);
uint16_t, <=mmap_file" class=sss="sref">uint16_t   22#include <stom_sortf="+code=wbe" class="sre2">wbe225> += 4ss="sref">i);
uint64_t, <=mmap_file" class=sss="sref">uint64_t   22#include <eturn munmap(  92        }
2put_una}a href="scripts/sortextableend  190       2f="script2/sortextable.c#L129" id=2L129"22 == jmpenv&/LFMAref">sb.i);
mmap_fil_t" class="s-cripa href="+code=e"_idtext">mmap_fil_t" c_idtexs="s href="scripts/sor/LFMAref">sb.i<) != 0line" name="L190"> 190       2f="script2f="+code=w2be" class="sr2f">w2229able_sort_t<||);
uint16_t<(mmap_fil_t" class="s-cripa href="+code=e"_">tat">mmap_fil_t" c_">ta">i<) != lass="string">&/T_EXECef">sb. 200mmap_fil_t" class="s-cripa href="+code=e"_idtext">mmap_fil_t" c_idtexs="s[*ehdr&/V_CURRENTss="sref">ehdr 192       2fail_file();
stderr, "Could not mmap file: %s\n", 
fname);
  91          2f="script2/sortextable.c#L133" id=2L133"23ef">uint32_t *  92        }
2oid w823a>( 197
 165       2put_unaligned_le64ipts/sortextable.c#L202" id="L202" class==mmap_file" class=NULble.c#L65" id="LNULb   22#include &lf="script2/sortextable.c#L137" id=2L137"23">munmap(uint16_t<(mmap_fil_t" class="s-cripa href="+code=e"_mach.c#L1>mmap_fil_t" c_mach.c#ss="ls="line" name="L192"> 192       2wle23sref">put_unadefault:line" name="L192"> 192       2f="script2/sortextable.c#L139" id=2L139"23able.c#L169" id="L169
stderr, "Could not mmap file: %s\n", fndef">fname);
 192       2mmap_faileduint16_t<(mmap_fil_t" class="s-cripa href="+code=e"_mach.c#L1>mmap_fil_t" c_mach.c#ss="lf="scripts/sortextable.c#L91" id="L91" class="line" name="L91">  91          2f="script2/sortextable.c#L141" id=2L141"24ref">fail_file();
  81        }
2oid w224ref">fail_file();breakine" name="L92">  92        }
2f="script2/sortextable.c#L143" id=2L143"24ef">uint32_t      183
 192       2put_unacas>      183
 192       2<="script2/sortextable.c#L145" id=2L145"244ref">put_unacas>      183
 192       2 += 4ss="sref">i);
(char *extab_iine" name="L92">  92        }
2f="script2ode=uint64_t" class="sre2">uin24">munmap(  92        }
2a href="+2ode=uint32_t" class="sre2">uin247ref">put_unacas>      183
 192       2<="script2ode=uint16_t" class="sre2">uin248ref">put_unacas>      183
 192       2w8249able_sort_t      183
 192       2oid (*wfprintf(  92        }
2oid (*w225L82" class="lia href="scripts/sortextableend  190       2="scripts2sortextable.c#L153" id="2153" 2lass="line" name="L153"> 153typedef2void (*put_unammap_fil_t" class="s-cripa href="+code=e"_idtext">mmap_fil_t" c_idtexs="s[* 192       2="scripts2sortextable.c#L155" id="2155" 254ref">put_unadefault:line" name="L192"> 192       2ass="comm2nt">/* 32 bit and 64 bit2are v255> += 4ss="sref">i);
stderr, "Could not mmap file: %s\n", fndef">fname);
 192       2 "munmap(i);
mmap_fil_t" class="s-cripa href="+code=e"_idtext">mmap_fil_t" c_idtexs="s[*  91          2fd_map, 0);"scripts/sortexts/sortextable.c#L81" id="L81" class="line" name="L81">  81        }
2 "  92        }
2="scripts2sortextable.c#L160" id="2160" 259able_sort_t      183
 192       2nt fprintf(uint16_t<(mmap_fil_t" class="s-cripa href="+code=e"_ehortextable.c#L192" "_ehorte">i<) != orteofclass="string">&/ehdr" class="sref">ehdr =  200{
fail_file();||);
uint16_t<(mmap_fil_t" class="s-cripa href="+code=e"_shtexortextable.c#L192" "_shtexorte">i<) != orteofclass="string">&/ehdr"Sclass="sref">ehdr = Sa href="s="line" name="L192"> 192       2int26ef">uint32_t *i);
stderr, "Could not mmap file: %sline" name="L192"> 192       2int26a>( *i", 
ref">fname);
  91          2="scripts2sortextable.c#L165" id="2165" 26a> += 4ss="sref">i, 0);"scripts/sortexts/sortextable.c#L81" id="L81" class="line" name="L81">  81        }
2if (i 197
&t0" f">mmap_fil_t" class="sf="scripts/sortextable.c#L91" id="L91" class="ompare_relative_tapts/sortextable.c#L202" id="L202" class=line" name="L81">  81        }
2ia href="2f="+code=av" class="sref2>avfd_map, 0);breakine" name="L92">  92        }
2        r2turn 1;
put_unacas>      183
 192       2<"scripts2mmap_failed = 1;
64" class="sref">ehdr64" clasref" href="scripts/sortextgclass="sref">ehdr(&/eh64" class="sref">ehdr64" clasref"  class="sref">b<"sref">mmap_fil_t" class="sref">uint32_t< fprintf(uint16_t<(ehdr-cripa href="+code=e"_ehortextable.c#L192" "_ehorte">i<) != orteofclass="string">&/eh64" class="sref">ehdr64" clasref"line" name="L200"> 200{
fail_file();||);
uint16_t<(ehdr-cripa href="+code=e"_shtexortextable.c#L192" "_shtexorte">i<) != orteofclass="string">&/eh64"Sclass="sref">ehdr64"Sclaref="s="line" name="L192"> 192       2oid uint32_t *i);
stderr, "Could not mmap file: %sline" name="L192"> 192       2f="script2/sortextable.c#L174" id=2L174"27a>( *i", 
ref">fname);
  91          2int 27a> += 4ss="sref">i, 0);"scripts/sortexts/sortextable.c#L81" id="L81" class="line" name="L81">  81        }
2="scripts2sortextable.c#L176" id="2176" 275> += 4ss="sref">i 197
/*
&gclass="sref">ehdrf="scripts/sortextable.c#L91" id="L91" class="ompare_relative_tapts/sortextable.c#L202" id="L202" class=line" name="L81">  81        }
2ass="comm2nt">         * Do the sa2e thi27sref">fd_map, 0);breakine" name="L92">  92        }
2ass="comm2nt">         * being rel2tive 278ref">put_una"line" name="L197"> 197
         */
<2 href27"sref">mmap_fia href="scripts/sortextableend  190       2i = 028ass="line" name="L121"> 121static 2while (i<28table_sort_t jmpenv  81        }
2        <2 href="+code=uint32_t" c2ass="28lass="line" name="L133"> 133static 2        <2 href="+code=w" class="s2ef">w28lass="line" name="L200"> 200i2/a> +=, <"line" name="L200"> 200jmpenv, int bv)
 200{
 127       2(ehdr( 190       2( 175
/* Now denor2alize28ass="line" name="L160"> 160static 2i = 02
)2s="line" name="L192"> 192       2while (i<29ref">fail_file();
stderr, "Could not mmap file: %s\n", );
  81        }
2        <2 href="+code=uint32_t" c2ass="2ref">uint32_t * 170}
w29a>( 197
i29ass="line" name="L165"> 165       2}

 190       2f="script2/sortextable.c#L197" id=2L197"29table.c#L167"forode=image_size" class="sref">image_size= 1>) {
image_size) {
) {
image_siz++s="line" name="L192"> 192       2wa href="2sortextable.c#L198" id="2198" 29sref">fd_map, 0);age, int bv)
image_siz]"line" name="L170"> 170}
x);
ass==mmap_file" class=aetjss="sref">jmpenv&jssenref">bv)
  81        }
2"+code=do2file" class="sref">do_fi2e29ass="line" name="L160"> 160static 3f="script3/sortextable.c#L201" id=3L201"30f">fprintf(x);
ass=s="line" name="L192"> 192       3fail_file();default:line" name="L192"> 192       330ef">uint32_t *i);
stderr, "Could not mmap file: %s\n", fname);
  81        }
3scripts/s3rtextable.c#L44204" id="3L200"30a>( *mmap_fil_t" cxixpan c1line" name="L81">  81        }
3s4ripts/s3rhref="+code=i" class="s3f="+c30a> += 4ss="sref">i, 0);breakine" name="L92">  92        }
3ustom_sor3" class="0"1n65 168 3     305> += 4ss="sref">i      183
mmap_fil_t" SJ_SETJMPable:= s=href="scripts/sortextable0" id= sequence0" class="line" name="L190"> 190       3return munmap(iref="scripts/sortextableAort_rpro   msi="+early cleanus()0" class="line" name="L190"> 190       3r7turn fd_map, 0);ef">i);
jmpenv 167       3rt;uin30able.c#L169" id="L169t32_t" class="sref">uint0" class="liuint0" class="l-f="+code=mmap_file" class=NULble.c#L65" id="LNULb   22#include &3f="script3/sortextable.c#L110" id=3L110"30"sref">mmap_faileduin/a>);
"L8ed="sref">jmpenv);
"L8ed_size= 1>ne" name="L22">  22#include &3return fprintf(uinhref="+code=fname" class="sref">fn"scripts/sortexttextable.c#L81" id= class=line" name="L81">  81        }
3rt;fail_file();;;;;;;;;breakine" name="L92">  92        }
3a href="+3ode=uint32_t" class="sre3">uin31ef">uint32_t *      183
 190       3rcripts/s3/sortextable.c#L114" id=3L114"31a>(ehdrine" name="L92">  92        }
3a4ripts/s3href="+code=get_unaligne3_le3231a> += 4ss="sref">i  92        }
3astom_sor3/sortextable.c#L116" id=3L116"315> += 4ss="sref">i      183
mmap_fil_t" SJ_SUCCEEDable:= s=href="scripts/sortextablepremaclae success " class="line" name="L190"> 190       3return uin31">munmap( 190       3r7turn fd_map, 0);;;;;;;;;breakine" name="L92">  92        }
3at; 190       3r="script3/sortextable.c#L120" id=3L120"319able_sort_t<"line" name="L197"> 197
fprintfehdrine" name="L92">  92        }
3tt;w832ref">"line" name="L197"> 197
uin32ef">u


The oaiginal LXR softw.c#Lby9" idine" namehttp:/> 1urceforge.net/projec7">lxr">LXR /sorunity#37;s\lxr@=i"ux.no#37;.
lxr.=i"ux.no kindly hostedLby9ine" namehttp:/>www.redpill-=i"pro.no">Redpill Li"pro AS#37;s\provie: L179Li"ux/hrefult hresortopera179"s services since01995.