~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/objtool/special.c

Version: ~ [ linux-5.16 ] ~ [ linux-5.15.13 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.90 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.170 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.224 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.261 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.296 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.298 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU General Public License
  6  * as published by the Free Software Foundation; either version 2
  7  * of the License, or (at your option) any later version.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  * GNU General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU General Public License
 15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
 16  */
 17 
 18 /*
 19  * This file reads all the special sections which have alternate instructions
 20  * which can be patched in or redirected to at runtime.
 21  */
 22 
 23 #include <stdlib.h>
 24 #include <string.h>
 25 
 26 #include "special.h"
 27 #include "warn.h"
 28 
 29 #define EX_ENTRY_SIZE           12
 30 #define EX_ORIG_OFFSET          0
 31 #define EX_NEW_OFFSET           4
 32 
 33 #define JUMP_ENTRY_SIZE         24
 34 #define JUMP_ORIG_OFFSET        0
 35 #define JUMP_NEW_OFFSET         8
 36 
 37 #define ALT_ENTRY_SIZE          13
 38 #define ALT_ORIG_OFFSET         0
 39 #define ALT_NEW_OFFSET          4
 40 #define ALT_FEATURE_OFFSET      8
 41 #define ALT_ORIG_LEN_OFFSET     10
 42 #define ALT_NEW_LEN_OFFSET      11
 43 
 44 #define X86_FEATURE_POPCNT (4*32+23)
 45 
 46 struct special_entry {
 47         const char *sec;
 48         bool group, jump_or_nop;
 49         unsigned char size, orig, new;
 50         unsigned char orig_len, new_len; /* group only */
 51         unsigned char feature; /* ALTERNATIVE macro CPU feature */
 52 };
 53 
 54 struct special_entry entries[] = {
 55         {
 56                 .sec = ".altinstructions",
 57                 .group = true,
 58                 .size = ALT_ENTRY_SIZE,
 59                 .orig = ALT_ORIG_OFFSET,
 60                 .orig_len = ALT_ORIG_LEN_OFFSET,
 61                 .new = ALT_NEW_OFFSET,
 62                 .new_len = ALT_NEW_LEN_OFFSET,
 63                 .feature = ALT_FEATURE_OFFSET,
 64         },
 65         {
 66                 .sec = "__jump_table",
 67                 .jump_or_nop = true,
 68                 .size = JUMP_ENTRY_SIZE,
 69                 .orig = JUMP_ORIG_OFFSET,
 70                 .new = JUMP_NEW_OFFSET,
 71         },
 72         {
 73                 .sec = "__ex_table",
 74                 .size = EX_ENTRY_SIZE,
 75                 .orig = EX_ORIG_OFFSET,
 76                 .new = EX_NEW_OFFSET,
 77         },
 78         {},
 79 };
 80 
 81 static int get_alt_entry(struct elf *elf, struct special_entry *entry,
 82                          struct section *sec, int idx,
 83                          struct special_alt *alt)
 84 {
 85         struct rela *orig_rela, *new_rela;
 86         unsigned long offset;
 87 
 88         offset = idx * entry->size;
 89 
 90         alt->group = entry->group;
 91         alt->jump_or_nop = entry->jump_or_nop;
 92 
 93         if (alt->group) {
 94                 alt->orig_len = *(unsigned char *)(sec->data->d_buf + offset +
 95                                                    entry->orig_len);
 96                 alt->new_len = *(unsigned char *)(sec->data->d_buf + offset +
 97                                                   entry->new_len);
 98         }
 99 
100         if (entry->feature) {
101                 unsigned short feature;
102 
103                 feature = *(unsigned short *)(sec->data->d_buf + offset +
104                                               entry->feature);
105 
106                 /*
107                  * It has been requested that we don't validate the !POPCNT
108                  * feature path which is a "very very small percentage of
109                  * machines".
110                  */
111                 if (feature == X86_FEATURE_POPCNT)
112                         alt->skip_orig = true;
113         }
114 
115         orig_rela = find_rela_by_dest(sec, offset + entry->orig);
116         if (!orig_rela) {
117                 WARN_FUNC("can't find orig rela", sec, offset + entry->orig);
118                 return -1;
119         }
120         if (orig_rela->sym->type != STT_SECTION) {
121                 WARN_FUNC("don't know how to handle non-section rela symbol %s",
122                            sec, offset + entry->orig, orig_rela->sym->name);
123                 return -1;
124         }
125 
126         alt->orig_sec = orig_rela->sym->sec;
127         alt->orig_off = orig_rela->addend;
128 
129         if (!entry->group || alt->new_len) {
130                 new_rela = find_rela_by_dest(sec, offset + entry->new);
131                 if (!new_rela) {
132                         WARN_FUNC("can't find new rela",
133                                   sec, offset + entry->new);
134                         return -1;
135                 }
136 
137                 alt->new_sec = new_rela->sym->sec;
138                 alt->new_off = (unsigned int)new_rela->addend;
139 
140                 /* _ASM_EXTABLE_EX hack */
141                 if (alt->new_off >= 0x7ffffff0)
142                         alt->new_off -= 0x7ffffff0;
143         }
144 
145         return 0;
146 }
147 
148 /*
149  * Read all the special sections and create a list of special_alt structs which
150  * describe all the alternate instructions which can be patched in or
151  * redirected to at runtime.
152  */
153 int special_get_alts(struct elf *elf, struct list_head *alts)
154 {
155         struct special_entry *entry;
156         struct section *sec;
157         unsigned int nr_entries;
158         struct special_alt *alt;
159         int idx, ret;
160 
161         INIT_LIST_HEAD(alts);
162 
163         for (entry = entries; entry->sec; entry++) {
164                 sec = find_section_by_name(elf, entry->sec);
165                 if (!sec)
166                         continue;
167 
168                 if (sec->len % entry->size != 0) {
169                         WARN("%s size not a multiple of %d",
170                              sec->name, entry->size);
171                         return -1;
172                 }
173 
174                 nr_entries = sec->len / entry->size;
175 
176                 for (idx = 0; idx < nr_entries; idx++) {
177                         alt = malloc(sizeof(*alt));
178                         if (!alt) {
179                                 WARN("malloc failed");
180                                 return -1;
181                         }
182                         memset(alt, 0, sizeof(*alt));
183 
184                         ret = get_alt_entry(elf, entry, sec, idx, alt);
185                         if (ret)
186                                 return ret;
187 
188                         list_add_tail(&alt->list, alts);
189                 }
190         }
191 
192         return 0;
193 }
194 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp