1 /* ld.so error exception allocation and deallocation.
2 Copyright (C) 1995-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
27 /* This message we return as a last resort. We define the string in a
28 variable since we have to avoid freeing it and so have to enable
29 a pointer comparison. See below and in dlfcn/dlerror.c. */
30 static const char _dl_out_of_memory
[] = "out of memory";
32 /* Dummy allocation object used if allocating the message buffer
35 oom_exception (struct dl_exception
*exception
)
37 exception
->objname
= "";
38 exception
->errstring
= _dl_out_of_memory
;
39 exception
->message_buffer
= NULL
;
43 __attribute__ ((noreturn
))
44 length_mismatch (void)
46 _dl_fatal_printf ("Fatal error: "
47 "length accounting in _dl_exception_create_format\n");
50 /* Adjust the message buffer to indicate whether it is possible to
51 free it. EXCEPTION->errstring must be a potentially deallocatable
54 adjust_message_buffer (struct dl_exception
*exception
)
56 /* If the main executable is relocated it means the libc's malloc
60 malloced
= (GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
!= NULL
61 && (GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
->l_relocated
!= 0));
64 exception
->message_buffer
= (char *) exception
->errstring
;
66 exception
->message_buffer
= NULL
;
70 _dl_exception_create (struct dl_exception
*exception
, const char *objname
,
71 const char *errstring
)
75 size_t len_objname
= strlen (objname
) + 1;
76 size_t len_errstring
= strlen (errstring
) + 1;
77 char *errstring_copy
= malloc (len_objname
+ len_errstring
);
78 if (errstring_copy
!= NULL
)
80 /* Make a copy of the object file name and the error string. */
81 exception
->objname
= memcpy (__mempcpy (errstring_copy
,
82 errstring
, len_errstring
),
83 objname
, len_objname
);
84 exception
->errstring
= errstring_copy
;
85 adjust_message_buffer (exception
);
88 oom_exception (exception
);
90 rtld_hidden_def (_dl_exception_create
)
93 _dl_exception_create_format (struct dl_exception
*exception
, const char *objname
,
98 size_t len_objname
= strlen (objname
) + 1;
99 /* Compute the length of the result. Include room for two NUL
101 size_t length
= len_objname
+ 1;
105 for (const char *p
= fmt
; *p
!= '\0'; ++p
)
112 length
+= strlen (va_arg (ap
, const char *));
115 /* Assumed to be '%'. */
125 if (length
> PTRDIFF_MAX
)
127 oom_exception (exception
);
130 char *errstring
= malloc (length
);
131 if (errstring
== NULL
)
133 oom_exception (exception
);
136 exception
->errstring
= errstring
;
137 adjust_message_buffer (exception
);
139 /* Copy the error message to errstring. */
141 /* Next byte to be written in errstring. */
142 char *wptr
= errstring
;
143 /* End of the allocated string. */
144 char *const end
= errstring
+ length
;
149 for (const char *p
= fmt
; *p
!= '\0'; ++p
)
157 const char *ptr
= va_arg (ap
, const char *);
158 size_t len_ptr
= strlen (ptr
);
159 if (len_ptr
> end
- wptr
)
161 wptr
= __mempcpy (wptr
, ptr
, len_ptr
);
171 _dl_fatal_printf ("Fatal error:"
172 " invalid format in exception string\n");
187 if (len_objname
!= end
- wptr
)
189 exception
->objname
= memcpy (wptr
, objname
, len_objname
);
192 rtld_hidden_def (_dl_exception_create_format
)
195 _dl_exception_free (struct dl_exception
*exception
)
197 free (exception
->message_buffer
);
198 exception
->objname
= NULL
;
199 exception
->errstring
= NULL
;
200 exception
->message_buffer
= NULL
;
202 rtld_hidden_def (_dl_exception_free
)