Home | History | Annotate | Download | only in bionic
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *  * Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  *  * Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in
     12  *    the documentation and/or other materials provided with the
     13  *    distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <private/bionic_asm.h>
     30 
     31     .syntax unified
     32     .fpu    neon
     33     .thumb
     34     .thumb_func
     35 
     36 // Get the length of src string, then get the source of the dst string.
     37 // Check that the two lengths together don't exceed the threshold, then
     38 // do a memcpy of the data.
     39 ENTRY(__strcat_chk)
     40     pld     [r0, #0]
     41     push    {r0, lr}
     42     .cfi_def_cfa_offset 8
     43     .cfi_rel_offset r0, 0
     44     .cfi_rel_offset lr, 4
     45     push    {r4, r5}
     46     .cfi_adjust_cfa_offset 8
     47     .cfi_rel_offset r4, 0
     48     .cfi_rel_offset r5, 4
     49 
     50     mov     lr, r2
     51 
     52     // Save the dst register to r5
     53     mov     r5, r0
     54 
     55     // Zero out r4
     56     eor     r4, r4, r4
     57 
     58     // r1 contains the address of the string to count.
     59 .L_strlen_start:
     60     mov     r0, r1
     61 
     62     ands    r3, r0, #7
     63     bne     .L_align_src
     64 
     65     .p2align 2
     66 .L_mainloop:
     67     ldmia   r1!, {r2, r3}
     68 
     69     pld     [r1, #64]
     70 
     71     sub     ip, r2, #0x01010101
     72     bic     ip, ip, r2
     73     ands    ip, ip, #0x80808080
     74     bne     .L_zero_in_first_register
     75 
     76     sub     ip, r3, #0x01010101
     77     bic     ip, ip, r3
     78     ands    ip, ip, #0x80808080
     79     bne     .L_zero_in_second_register
     80     b       .L_mainloop
     81 
     82 .L_zero_in_first_register:
     83     sub     r3, r1, r0
     84     // Check for zero in byte 0.
     85     lsls    r2, ip, #17
     86     beq     .L_check_byte1_reg1
     87 
     88     sub     r3, r3, #8
     89     b       .L_finish
     90 
     91 .L_check_byte1_reg1:
     92     bcc     .L_check_byte2_reg1
     93 
     94     sub     r3, r3, #7
     95     b       .L_finish
     96 
     97 .L_check_byte2_reg1:
     98     // Check for zero in byte 2.
     99     tst     ip, #0x800000
    100     it      ne
    101     subne   r3, r3, #6
    102     bne     .L_finish
    103     sub     r3, r3, #5
    104     b       .L_finish
    105 
    106 .L_zero_in_second_register:
    107     sub     r3, r1, r0
    108     // Check for zero in byte 0.
    109     lsls    r2, ip, #17
    110     beq     .L_check_byte1_reg2
    111 
    112     sub     r3, r3, #4
    113     b       .L_finish
    114 
    115 .L_check_byte1_reg2:
    116     bcc     .L_check_byte2_reg2
    117 
    118     sub     r3, r3, #3
    119     b       .L_finish
    120 
    121 .L_check_byte2_reg2:
    122     // Check for zero in byte 2.
    123     tst     ip, #0x800000
    124     it      ne
    125     subne   r3, r3, #2
    126     bne     .L_finish
    127     sub     r3, r3, #1
    128     b       .L_finish
    129 
    130 .L_align_src:
    131     // Align to a double word (64 bits).
    132     rsb     r3, r3, #8
    133     lsls    ip, r3, #31
    134     beq     .L_align_to_32
    135 
    136     ldrb    r2, [r1], #1
    137     cbz     r2, .L_done
    138 
    139 .L_align_to_32:
    140     bcc     .L_align_to_64
    141 
    142     ldrb    r2, [r1], #1
    143     cbz     r2, .L_done
    144     ldrb    r2, [r1], #1
    145     cbz     r2, .L_done
    146 
    147 .L_align_to_64:
    148     tst     r3, #4
    149     beq     .L_mainloop
    150     ldr     r2, [r1], #4
    151 
    152     sub     ip, r2, #0x01010101
    153     bic     ip, ip, r2
    154     ands    ip, ip, #0x80808080
    155     bne     .L_zero_in_second_register
    156     b       .L_mainloop
    157 
    158 .L_done:
    159     sub     r3, r1, r0
    160     sub     r3, r3, #1
    161 
    162 .L_finish:
    163     cmp     r4, #0
    164     bne     .L_strlen_done
    165 
    166     // Time to get the dst string length.
    167     mov     r1, r5
    168 
    169     // Save the original source address to r5.
    170     mov     r5, r0
    171 
    172     // Save the current length (adding 1 for the terminator).
    173     add     r4, r3, #1
    174     b       .L_strlen_start
    175 
    176     // r0 holds the pointer to the dst string.
    177     // r3 holds the dst string length.
    178     // r4 holds the src string length + 1.
    179 .L_strlen_done:
    180     add     r2, r3, r4
    181     cmp     r2, lr
    182     itt     hi
    183     movhi   r0, lr
    184     bhi     __strcat_chk_fail
    185 
    186     // Set up the registers for the memcpy code.
    187     mov     r1, r5
    188     pld     [r1, #64]
    189     mov     r2, r4
    190     add     r0, r0, r3
    191     pop     {r4, r5}
    192 
    193     // Fall through into the memcpy_base function.
    194 END(__strcat_chk)
    195 
    196 #define MEMCPY_BASE         __strcat_chk_memcpy_base
    197 #define MEMCPY_BASE_ALIGNED __strcat_chk_memcpy_base_aligned
    198 #include "memcpy_base.S"
    199