00001 /* 00002 * Copyright 2005 PathScale, Inc. All Rights Reserved. 00003 */ 00004 00005 /* 00006 newscn.c - implementation of the elf_newscn(3) function. 00007 Copyright (C) 1995 - 1998, 2003 Michael Riepe <michael@stud.uni-hannover.de> 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Library General Public 00011 License as published by the Free Software Foundation; either 00012 version 2 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Library General Public License for more details. 00018 00019 You should have received a copy of the GNU Library General Public 00020 License along with this library; if not, write to the Free Software 00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00022 */ 00023 00024 #include <private.h> 00025 00026 #ifndef lint 00027 static const char rcsid[] = "@(#) $Id: newscn.c,v 1.1.1.1 2005/10/21 19:00:00 marcel Exp $"; 00028 #endif /* lint */ 00029 00030 int 00031 _elf_update_shnum(Elf *elf, size_t shnum) { 00032 Elf_Scn *scn; 00033 00034 if (elf->e_class == ELFCLASS32) { 00035 if (shnum >= SHN_LORESERVE) { 00036 scn = elf->e_scn_1; 00037 elf_assert(scn->s_index == 0); 00038 scn->s_shdr_flags |= ELF_F_DIRTY; 00039 scn->s_shdr32.sh_size = shnum; 00040 shnum = 0; 00041 } 00042 elf->e_ehdr_flags |= ELF_F_DIRTY; 00043 ((Elf32_Ehdr*)elf->e_ehdr)->e_shnum = shnum; 00044 return 0; 00045 } 00046 #if __LIBELF64 00047 else if (elf->e_class == ELFCLASS64) { 00048 if (shnum >= SHN_LORESERVE) { 00049 scn = elf->e_scn_1; 00050 elf_assert(scn->s_index == 0); 00051 scn->s_shdr_flags |= ELF_F_DIRTY; 00052 scn->s_shdr64.sh_size = shnum; 00053 shnum = 0; 00054 } 00055 elf->e_ehdr_flags |= ELF_F_DIRTY; 00056 ((Elf64_Ehdr*)elf->e_ehdr)->e_shnum = shnum; 00057 return 0; 00058 } 00059 #endif /* __LIBELF64 */ 00060 else if (valid_class(elf->e_class)) { 00061 seterr(ERROR_UNIMPLEMENTED); 00062 } 00063 else { 00064 seterr(ERROR_UNKNOWN_CLASS); 00065 } 00066 return -1; 00067 } 00068 00069 static Elf_Scn* 00070 _buildscn(Elf *elf) { 00071 Elf_Scn *scn; 00072 00073 elf_assert(elf); 00074 elf_assert(elf->e_ehdr); 00075 elf_assert(_elf_scn_init.s_magic == SCN_MAGIC); 00076 while ((scn = (Elf_Scn*)malloc(sizeof(*scn)))) { 00077 *scn = _elf_scn_init; 00078 scn->s_elf = elf; 00079 scn->s_scn_flags = ELF_F_DIRTY; 00080 scn->s_shdr_flags = ELF_F_DIRTY; 00081 scn->s_freeme = 1; 00082 if (elf->e_scn_n) { 00083 scn->s_index = elf->e_scn_n->s_index + 1; 00084 if (_elf_update_shnum(elf, scn->s_index + 1)) { 00085 free(scn); 00086 return NULL; 00087 } 00088 elf->e_scn_n->s_link = scn; 00089 elf->e_scn_n = scn; 00090 return scn; 00091 } 00092 scn->s_index = 0; 00093 if (_elf_update_shnum(elf, 1)) { 00094 free(scn); 00095 return NULL; 00096 } 00097 elf->e_scn_1 = elf->e_scn_n = scn; 00098 } 00099 seterr(ERROR_MEM_SCN); 00100 return NULL; 00101 } 00102 00103 Elf_Scn* 00104 elf_newscn(Elf *elf) { 00105 Elf_Scn *scn; 00106 00107 if (!elf) { 00108 return NULL; 00109 } 00110 elf_assert(elf->e_magic == ELF_MAGIC); 00111 if (!elf->e_readable && !elf->e_ehdr) { 00112 seterr(ERROR_NOEHDR); 00113 } 00114 else if (elf->e_kind != ELF_K_ELF) { 00115 seterr(ERROR_NOTELF); 00116 } 00117 else if (!elf->e_ehdr && !_elf_cook(elf)) { 00118 return NULL; 00119 } 00120 else if ((scn = _buildscn(elf))) { 00121 return scn; 00122 } 00123 return NULL; 00124 }
1.5.6